创建新对象并对其进行初始化

时间:2019-10-10 13:04:17

标签: java object

分配问题是为类玩家定义一个构造函数,它要求输入玩家名称(我想我做对了),并将位置初始化为零,并使用新的Jar( );

因此,我创建了2个类,一个类称为Jar,一个类称为Player,基本上,一个播放器Jar Position表示为0,一个播放器Stone表示为null。 (玩家位置和Jar位置不同)

public class Jar
{
    public static int position;
    public static Jar stone;

    /**
     * Constructor for objects of class Jar
     */
    public Jar()
    {
        this.position = 0;
        this.stone = null;

    }
}



import java.util.Scanner;

public class Player
{
    // instance variables - replace the example below with your own
    private String name ;
    private int position;
    private Player JarPosition;
    private Player JarStone;


    /**
     * Constructor for objects of class Player
     */
    public Player()
    {
       System.out.print("Enter player's name: ");
       name = Global.keyboard.nextLine();

       this.position = 0;

       Jar jar = new Jar();
       JarPosition = jar.position;
       JarStone = jar.stone;

1 个答案:

答案 0 :(得分:0)

让它逐步进行。

首先,您使用main方法启动程序:

public static void main(String[] args){
     // execute this code here
}

您可以将这种方法放在您喜欢的任何类中,尽管我建议您将其放在一个类中,该类将用于“控制”程序的流程:

public class MyJarProgram{

    public static void main(String[] args){

    }   
}

现在要摆脱所有static和非静态的混乱,让我们继续“ Objects”(类的实例)。意味着此main方法是您唯一需要的静态方法。

现在您要开始构造您的球员,罐子(我真的不知道那是什么,但请记住:),位置和所有东西,因此您可能要开始询问用户输入球员的名字:

 public static void main(String[] args){
     System.out.print("Please enter the player's name");   
     String playerName = Global.keyboard.nextLine();    // I guess this method works like this, so your variable playerName should now contain the user's input

     // now you can instantiate your player object
     Player player = new Player(playerName);   // here we need a constructor of the class player that takes the name.

 }

因此,现在您必须创建Player类,并为其指定构造函数名称

public class Player{

      private String name;    // make all variables in your classes non-static and private to be on the save-side

      public Player(String name){
          this.name = name;  // you take the parameter name and set the private member variable name to the same value
      }

      // since your variable name is private to the class player you might want to add some standard getter and setter methods like this:
      public String getName(){
          return name;
      }

      public void setName(String name){
           this.name = name;         
      }

}

使用播放器name进行操作时,也可以使用position进行操作。给Player类提供另一个成员变量position,并将必要的代码添加到构造函数中(可能还包括getter和setter)。

然后您的主要方法如下:

 public static void main(String[] args){
     System.out.print("Please enter the player's name");   
     String playerName = Global.keyboard.nextLine();    

     Player player = new Player(playerName, 0);  // creates the player with his name and position.
     System.out.println("Player :"+player.getName()+" is on position "+player.getPosition());   // and that's how you can access the players attributes with your getters and setters

 }

尽管我猜您可能想给玩家默认位置0,在这种情况下,您无需在构造函数中传递位置,而是将位置设置为0:

 public Player(String name){
     this.name = name;     // takes the parameter to init the name
     this.position = 0;    // will initialize the position by default with 0
 }

我希望这可以帮助您更好地了解构造函数,类和内容。祝你好运!