简体   繁体   English

构造函数中的Java成员初始化

[英]Java member initialization in constructors

I'm taking my first steps into a new programming language after 4 years in c++, and I've chosen Java. 在使用c ++四年后,我正在迈出第一步,使用一种新的编程语言,并且选择了Java。 I am trying to make a simple tick tack toe game using classes, but I'm having a hard time understanding the differences between java and c++. 我正在尝试使用类制作一个简单的壁虱游戏,但我很难理解Java和C ++之间的区别。

in one java class file I have: 在一个java类文件中,我有:

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

In a separate java class file I have: 在一个单独的java类文件中,我有:

public class Player
{
    private String name;
    public Player(String name_in)
    {
        name = name_in;
    }
}

I want to know, how to initialize Player p1, p2; 我想知道如何初始化Player p1, p2; in the Game class, since I don't want to give the Player class a default constructor. Game类中,因为我不想给Player类一个默认的构造函数。 I'm sure I could just overload the constructors, like this: 我确定我可以重载构造函数,如下所示:

 public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

public class Player
{
    private String name;
    public Player() { }       
    public Player(String name_in)
    {
        name = name_in;
    }
}

But, I'm wondering if there is a way to just initialize those objects without having to declare them and then initialize them. 但是,我想知道是否有一种方法可以只初始化那些对象而不必先声明它们,然后再对其进行初始化。 ie, just initialize them. 即,只需初始化它们。 If it were c++ I would just do this: 如果是c ++,我将这样做:

TL;DR: the code below is c++, how would I do a similar "one step member initialization" in Java. TL; DR:下面的代码是c ++,我将如何在Java中执行类似的“一步成员初始化”。

class Game
{
    private:
    Player p1, p2;

    public:
    Game(std::string p1Name, std::string p2Name) : p1(p1Name), p2(p2Name) //<--One step member initialization
    {
        std::cout << p1Name << " vs. " << p2Name;
    }
}

class Player
{
    private:
    std::string name; 

    public:
    Player(String name_in) : name(name_in) { }  //<--Holy crap, another one.
}

There is no one step member initialization in Java. Java中没有一步成员初始化。 Your best bet is to construct the Player objects in Game 's constructor, 最好的选择是在Game的构造函数中构造Player对象,

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

or to receive the Player objects in Game 's constructor. 或在Game的构造函数中接收Player对象。

public class Game
{
    Player p1, p2;
    public Game(Player p1, Player p2)
    {
        this.p1 = p1; 
        this.p2 = p2; 
    }
}

In Java, there is no initializer like in C++, and there doesn't need to be one. 在Java中,没有像C ++中那样的初始化程序,也不需要一个初始化程序。 Your original code was ok: 您原来的代码还可以:

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

From your question, you seem to be under the impression that with the above code you would need to add an (undesired) default constructor for class Player. 从您的问题看来,您似乎有一种印象,就是使用上述代码,您需要为Player类添加一个(不需要的)默认构造函数。 Not so. 不是这样

Remember that, unlike similar C++ declarations, the Java variables p1 and p2 are references to objects, not objects themselves. 请记住,与类似的C ++声明不同,Java变量p1和p2是对对象的引用 ,而不是对象本身。 In the above code, p1 and p2 are first assigned to null and then, in the Game constructor, they are assigned to new objects. 在上面的代码中,首先将p1和p2分配为null,然后在Game构造函数中将它们分配给新对象。 This may seem wasteful, but if it's run enough times the JIT will probably optimize away the unnecessary assignments to null, and just assign p1 and p2 directly to new objects. 这似乎很浪费,但是如果运行足够的时间,JIT可能会优化掉不必要的分配给null,而直接将p1和p2分配给新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM