Java - 找不到符号构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14635606/
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
Java - Cannot find symbol constructor
提问by Jarand Boge
I'm completely new to Java, so I'm sorry if my question is dumb. Im working on this assignment, and I've been reading about main methods for hours now, but I just cant figure it out. I put some of my code below. I might be way off here, but what I'm hoping to accomplish is to get the main method to start the constructor, but when I compile I get an error saying "cannot find symbol - constructor Player". Now, Im guessing this has something to do with the string parameters of the constructor, but I'm all out. If anyone could shed some light on this, probably very simple problem, I'd be very happy :)
我对 Java 完全陌生,所以如果我的问题很愚蠢,我很抱歉。我正在完成这项任务,并且我已经阅读了几个小时的主要方法,但我无法弄清楚。我把我的一些代码放在下面。我可能离这里很远,但我希望完成的是获取启动构造函数的主要方法,但是当我编译时,我收到一条错误消息,提示“找不到符号 - 构造函数 Player”。现在,我猜这与构造函数的字符串参数有关,但我全力以赴。如果有人能对此有所了解,可能是很简单的问题,我会很高兴:)
public class Player {
private String nick;
private String type;
private int health;
public static void main(String[] args)
{
Player player = new Player();
player.print();
}
public Player(String nickName, String playerType)
{
nick = nickName;
type = playerType;
health = 100;
System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
}
public void print()
{
System.out.println("Name: " + nick);
System.out.println("Class: " + type);
System.out.println("Remanining Health: " + health);
}
回答by Reimeus
Player
has no no-arg constructor, you could use:
Player
没有无参数构造函数,您可以使用:
Player player = new Player("My Nickname", "Player Type");
If you wish to prompt the user for the Player
arguments, you can read like so:
如果你想提示用户输入Player
参数,你可以这样读:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Player Name:");
String nickName = scanner.nextLine();
System.out.print("Enter Player Type:");
String playerType = scanner.nextLine();
Player player = new Player(nickName, playerType);
回答by Rohit Jain
Clearly you are using 0-arg constructor
, when you haven't got one: -
显然你正在使用0-arg constructor
,当你还没有一个时:-
Player player = new Player();
Note that, when you provide a parameterized constructor in your class, the compiler will not add default constructor. You would have to add one 0-arg constructor manually, if you are using it.
请注意,当您在类中提供参数化构造函数时,编译器不会添加默认构造函数。如果您正在使用它,则必须手动添加一个 0-arg 构造函数。
So, either you can add one 0-arg constructor
as such: -
因此,您可以添加一个0-arg constructor
: -
public Player() {
this.nick = "";
this.type = "";
this.health = -1;
}
or, use the parameterized constructor to create the object.
或者,使用参数化构造函数来创建对象。
回答by kosa
When your class explicitly defines constructor, implicit no-arg constructor won't be created.
当您的类显式定义constructor 时,不会创建隐式无参数构造函数。
You have explicit constructor in your class
你的类中有显式构造函数
public Player(String nickName, String playerType)
{
nick = nickName;
type = playerType;
health = 100;
System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
}
And trying to invoke no-arg constructor
并尝试调用无参数构造函数
Player player = new Player();
Either you need to pass parameters in above code (or) create no-arg constructor.
您需要在上面的代码中传递参数(或)创建无参数构造函数。
回答by user1744361
A default constructor is created by java when a construtor is missing, this construtor simply calls the super class. When you defined an explicit constructor java wont create one. So you can either define one default constructor in your class e.g.
当缺少构造函数时,java 会创建一个默认构造函数,这个构造函数只是调用超类。当您定义一个显式构造函数时,java 不会创建一个。所以你可以在你的类中定义一个默认构造函数,例如
public Player()
{ nick = "abc";
type = "type";
health = 100;
System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
}
or modify the code to call the constructor u defined.
或修改代码以调用您定义的构造函数。
Player player = new Player("nick","type");
回答by mezzodrinker
What you tried to do in your main()
-method was to create a new Player object. But the problem is that you had to use the constructor you implemented (Player(String, String)
) but you used a constructor without any parameters (Player()
).
你试图在你的main()
-method 中做的是创建一个新的 Player 对象。但问题是你必须使用你实现的构造函数 ( Player(String, String)
) 但你使用了一个没有任何参数的构造函数 ( Player()
)。
You should either use empty strings (e.g. if you want to get a player dummy)
您应该使用空字符串(例如,如果您想获得玩家虚拟)
Player player = new Player("","");
or you should give the new player instance the name and type you want to, so for example
或者您应该为新玩家实例指定您想要的名称和类型,例如
Player player = new Player("flashdrive2049","Player");
Regards.
问候。