如何从java中的main方法调用构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26674509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a constructor from main method in java?
提问by Paw
I need to write a program to solve the eight queens problem and I have no idea how to do it, but I have already started with the multidimensional array. The thing is that I don't know how to call a constructor from the main class, just to check if the array was constructed properly, or how to call it from a method. Can anybody help, please? This is the code, it returns null.
我需要编写一个程序来解决八皇后问题,我不知道该怎么做,但我已经开始使用多维数组。问题是我不知道如何从主类调用构造函数,只是为了检查数组是否正确构造,或者如何从方法调用它。请问有人可以帮忙吗?这是代码,它返回空值。
import javax.swing.JOptionPane;
public class Eightqueens
{
private static int[][] board;
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,board);
}
public Eightqueens (int size)
{
size = 8;
board = new int[size][size];
int row;
int col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
board[row][col] = 0;
}
}
采纳答案by Eran
You are supposed to use the value passed to the constructor, not overwrite it :
您应该使用传递给构造函数的值,而不是覆盖它:
...
private int[][] board;
...
public Eightqueens (int size)
{
size = 8; // remove this
...
}
public int[][] getBoard()
{
return board;
}
public static void main(String[] args)
{
Eightqueens equeens = new Eightqueens (8); // create an instance
JOptionPane.showMessageDialog(null,equeens.getBoard()); // use accessor to get
// board
}
In addition, it doesn't make sense to initialize a static variable (board
) in the constructor, since each new instance you create would overwrite its value. I'd change it to non-static, and then you can get it from your instance by using an accessor method (equeens.getBoard()
).
此外,board
在构造函数中初始化静态变量 ( )没有意义,因为您创建的每个新实例都会覆盖其值。我会将其更改为非静态,然后您可以使用访问器方法 ( equeens.getBoard()
)从您的实例中获取它。
回答by Kevin DiTraglia
To call your constructor you must 'construct' a new object.
要调用构造函数,您必须“构造”一个新对象。
Eightqueens obj = new Eightqueens(5);
回答by aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
just when you instantiate your object of class with new keyword , you call your constructor. like this :
当您使用 new 关键字实例化您的类对象时,您会调用您的构造函数。像这样 :
public static void main(String[] args) {
Eightqueens eq = new Eightqueens(5);
}
回答by Michael Sanchez
You can start by calling your constructor by making a new instance of Eightqueens
你可以通过创建一个新的八皇后实例来调用你的构造函数
int size = 1; // anything
new Eightqueens(size);
回答by Dici
This code does not returnnull, it does not returnanything. You never call your constructor :
此代码不返回null,也不返回任何内容。你永远不会调用你的构造函数:
public static void main(String[] args) {
Eightqueens eq = new Eightqueens(5);
}
回答by shepard23
You can't call your constructor again. It only runs at the start of the code.
你不能再次调用你的构造函数。它只在代码的开头运行。
You can however, create a new instance of it to run it again.
但是,您可以创建它的新实例以再次运行它。
回答by dryairship
The Dialog Box is empty because the value of boardis nothing, which is because it has not been initialized. The boardget a value in the constructor method. So you have to call the constructor method before you show the dialog box.
Dialog Box 是空的,因为board的值是nothing,这是因为它没有被初始化。该板得到构造方法的值。所以你必须在显示对话框之前调用构造函数方法。
public static void main(String[] args)
{
Eightqueens eq = new Eightqueens(8);
JOptionPane.showMessageDialog(null,board);
}
Also, in the constructor method, you are overwriting the value of sizegiven by you earlier. Thus, there is practically no need to supply the variable to the method. So, either you remove the line:
此外,在构造函数方法中,您正在覆盖之前给定的size值。因此,实际上不需要向方法提供变量。因此,要么删除该行:
size = 8;
or, change the constructor method to not input the value, like this:
或者,将构造函数方法更改为不输入值,如下所示:
public Eightqueens ()
{
int size = 8;
board = new int[size][size];
int row;
int col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
board[row][col] = 0;
}