Java - Cannot find symbol constructor

点点圈 提交于 2019-12-20 03:33:20

问题


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 :)

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);
}

回答1:


Player has no no-arg constructor, you could use:

Player player = new Player("My Nickname", "Player Type");

If you wish to prompt the user for the Player arguments, you can read like so:

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);



回答2:


Clearly you are using 0-arg constructor, when you haven't got one: -

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.

So, either you can add one 0-arg constructor as such: -

public Player() {
    this.nick = "";
    this.type = "";
    this.health = -1;
}

or, use the parameterized constructor to create the object.




回答3:


When your class explicitly defines constructor, implicit no-arg constructor won't be created.

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.




回答4:


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()).

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.




回答5:


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.

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");


来源:https://stackoverflow.com/questions/14635606/java-cannot-find-symbol-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!