Java—尝试使用在另一个类的另一个构造函数中传递的参数初始化对象?
目前非常基础的java知识,在尝试创建一个Pacman游戏时需要一些帮助 目前我有三个类,player类,dot类和game类,它们相互作用来创建一个基本的Pacman游戏 我面临的问题是: 我需要如下所示的'initialX和initialY字段(这些将是用户输入的坐标) 在游戏类的新“玩家”对象中作为我的参数传递Java—尝试使用在另一个类的另一个构造函数中传递的参数初始化对象?,java,Java,目前非常基础的java知识,在尝试创建一个Pacman游戏时需要一些帮助 目前我有三个类,player类,dot类和game类,它们相互作用来创建一个基本的Pacman游戏 我面临的问题是: 我需要如下所示的'initialX和initialY字段(这些将是用户输入的坐标) 在游戏类的新“玩家”对象中作为我的参数传递 public class Game { public Game() { Player player = new Player();
public class Game
{
public Game()
{
Player player = new Player();
Dot dot1 = new Dot();
Dot dot2 = new Dot();
Dot dot3 = new Dot();
}
}
这让我很困惑,我想我不是走错了路,就是完全错过了什么 您声明的构造函数接受两个int参数,您调用它时不发送任何参数
Player player = new Player();
当使用下一行时,编译器将查找不带参数的默认构造函数,但它将失败,因为您没有声明默认构造函数,但声明了一个带两个int参数的构造函数
Player player = new Player();
这一行:
Player player = new Player(1, 2);// pass two int parameters t your defined constructor.
您可以在
Player
类中定义变量,然后将这些变量分配给同一类构造函数中的其他变量,如下所示:
public class Player
{
private int x, y, collectedDots;
int initialX = 15;
int initialY = 20;
public Player()
{
x = initialX;
y = initialY;
collectedDots = 0;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
public class Game
{
public Game()
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
Player player = new Player(i,j);
System.out.println(player.getX()); // prints out 15 in the console
System.out.println(player.getY()); // prints out 20 in the console
Dot dot1 = new Dot();
Dot dot2 = new Dot();
Dot dot3 = new Dot();
}
}
现在您的x
等于15,而y
等于20。但是这些值都是Player
类的一部分。但是,您可以在游戏
类中创建玩家
类的对象,并通过该对象可以访问x
和y
,如下所示:
public class Game
{
public Game()
{
Player player = new Player();
System.out.println(player.getX()); // prints out 15 in the console
System.out.println(player.getY()); // prints out 20 in the console
Dot dot1 = new Dot();
Dot dot2 = new Dot();
Dot dot3 = new Dot();
}
}
当然,您可以使用这些值做任何您想做的事情(比如将它们分配给游戏
类中的新变量并在那里操作它们)。我只是将它们打印出来,以便您可以看到它是来自Player
类的值
更新
您可以在创建Player
对象之前接受用户输入,然后将这些值传递给Player
类的构造函数,如下所示:
public class Player
{
private int x, y, collectedDots;
int initialX = 15;
int initialY = 20;
public Player()
{
x = initialX;
y = initialY;
collectedDots = 0;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
public class Game
{
public Game()
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
Player player = new Player(i,j);
System.out.println(player.getX()); // prints out 15 in the console
System.out.println(player.getY()); // prints out 20 in the console
Dot dot1 = new Dot();
Dot dot2 = new Dot();
Dot dot3 = new Dot();
}
}
当然,这是前提,如果你保持你的Player
类与你最初发布它的方式相同。但是,如果您希望继续能够更改x
和y
的值,则始终可以提供Setter方法。然后,您的班级将成为:
public class Player
{
private int x, y, collectedDots;
public Player(int initialX, int initialY)
{
x = initialX;
y = initialY;
collectedDots = 0;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setX(int newX)
{
x = newX;
}
public void setY(int newY)
{
y = newY;
}
}
无论何时,只要您想在Game
类中更改x
和y
的值,您只需执行以下操作(仅举一个示例):
同样,这只是一个例子。我并不是真的在构造函数中做所有这些,而是用它来解释如何做你想做的事情。构造函数需要两个
int
s,你是说:Player=newplayer(0,1)代码>或者其他什么?玩家会选择整数是,比如说(1,1)开始。但是它必须从传递给Player类中的构造函数的参数中获取这些初始值。。如果有意义的话?您可以在Player
类中定义int
变量。通过构造函数将它们分配给x
和y
。然后通过执行类似于player.getX()的操作,将这些值访问到Game
类
或player.getY()
假设player
是在Game
类中创建的player
类的实例。如果这不是你要问的,那么我恐怕很难理解你想要什么。因此,大致如下:Player Player=new Player(Player.getX(),Player.getY());?不,让我写下这个作为答案,这样你就能更好地理解我的意思。但是如果这两个整数是在Player类的Player构造函数中定义的,我如何将这些传递给Game类?只需使用getter方法访问Player类中声明的变量即可。@Wombat请告诉我这是否是您想要的。很难通过注释解释所有这些。但是如果没有参数,在编译代码后,我无法输入不同的x和y值?