Java - 尝试在另一个类中使用另一个构造函数中传递的参数初始化一个对象?

时间:2014-03-30 07:20:58

标签: java

目前非常基本的java知识,在尝试创建Pacman游戏时需要一些帮助。

目前我有三个类,即玩家类,点类和游戏类,所有这些类都可以创建Pacman的基本游戏。

我遇到的问题是:

我需要'initialX和initialY字段如下所示(这些将是用户输入的坐标);

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 class Game
{


    public Game()
    {
        Player player = new Player();
        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }


}

让我感到困惑,我假设我要么以错误的方式解决这个问题,要么我完全错过了一些东西。

2 个答案:

答案 0 :(得分:1)

您声明的构造函数接受两个int参数,并在不发送任何参数的情况下调用它。

当使用下面的行时,编译器将查找不带参数的默认构造函数,但是由于你没有声明默认值,它会失败,但你声明了一个带有两个int参数的构造函数。

Player player = new Player();

这一行:

Player player = new Player(1, 2);// pass two int parameters t your defined constructor.

答案 1 :(得分:0)

您可以在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;
    }

}

现在,您的x等于15,y等于20.但这些值都是Player类的一部分。但是,您在Player类中创建了Game类的对象,通过该对象,您可以访问xy,如下所示:

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

您当然可以使用这些值执行任何操作(例如将它们分配给Game类中的新变量并在那里操作它们)。我只是将它们打印出来,这样你就可以看到它来自Player类的值。

<强>更新

您可以在创建Player对象之前获取用户输入,然后将这些值传递给Player类的构造函数,如下所示:

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课程,就可以保留原来的发布方式。但是,如果您希望继续能够更改xy的值,则可以始终提供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;
    }
}

每当您想要从x课程更改yGame的值时,您只需执行以下操作(仅作为示例):< / p>

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

        player.setX(35);
        player.setY(48);
    }
}

同样,这只是一个例子。我不会在构造函数中完成所有这些操作,而只是用它来解释你如何做你想做的事。