简体   繁体   English

Java:声明空构造函数以从另一个类初始化对象的替代方法

[英]Java: Alternative to declaring empty constructor to initialize an object from another class

I am creating a simple, text-based interactive application, where the user is prompted for input. 我正在创建一个简单的基于文本的交互式应用程序,在该应用程序中会提示用户输入。 Each input corresponds to a specific command, which invokes a certain method. 每个输入都对应一个特定的命令,该命令调用特定的方法。 Because there are many methods, I have chosen to distribute them between different classes. 因为有很多方法,所以我选择在不同的类之间分配它们。 So the application works like this: 因此,应用程序的工作方式如下:

The main class is responsible for reading the user input (using a Scanner object). 主类负责读取用户输入(使用Scanner对象)。 The user input is then passed as a parameter to a method B in class B that decides which command the input corresponds to. 然后,将用户输入作为参数传递给类B中的方法B,该方法B确定输入对应于哪个命令。 After deciding, the method invokes the correct method, which can be in any of the other classes (in this example, method C in class C) 确定之后,该方法将调用正确的方法,该方法可以在其他任何类中(在此示例中,类C中的方法C)

This means that I have to initialize object instances in order to avoid null pointer exception. 这意味着我必须初始化对象实例,以避免出现空指针异常。 One of the classes that I have to initialize an object from only has a constructor with parameters, but I don't actually want to initialize an object with values in this case, but only to use it as a reference pointer to the object so I can invoke methods from that class. 我必须从中初始化对象的类之一只有一个带有参数的构造函数,但是在这种情况下,我实际上并不希望使用值初始化一个对象,而只是将其用作指向该对象的引用指针,所以我可以从该类调用方法。 Now I am using an empty constructor in order to solve this, but is there any alternative, better solution than declaring an empty constructor? 现在,我使用一个空的构造函数来解决此问题,但是有没有比声明一个空的构造函数更好的解决方案呢? I have written code that I think demonstrates my problem: 我写的代码证明了我的问题:

public class MainClass {
    ClassB classB = new ClassB();

    public void methodA {
        classB.methodB(userInput);
    }
}


public class ClassB {
    ClassC classC = new ClassC();

    public void methodB {
        classC.methodC();
    }
}


public class ClassC {
    String name;
    int age;

    public ClassC(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public ClassC() {
    }

    public void methodC() {
        // Do something
    }
}

but I don't actually want to initialize an object with values in this case, but only to use it as a reference pointer to the object so I can invoke methods from that class 但我实际上并不想在这种情况下用值初始化对象,而只是将其用作指向该对象的引用指针,以便可以从该类调用方法

The problem is that those methods normally need state of the object to make sense (ie the fields to be initialized). 问题在于那些方法通常需要对象的状态才有意义(即要初始化的字段)。 If in your case they don't, maybe it makes more sense to make the method static (and call ClassC.methodC() ) or put these methods on some other class with a default constructor. 如果您不这样做,则使该方法static (并调用ClassC.methodC() )或将这些方法放在具有默认构造函数的其他类上可能更有意义。

The real answer is: don't do that. 真正的答案是:不要那样做。 You see, if that ClassC is about storing details of a player ... first of call it Player (even when putting up examples: use meaningful names so people understand your intent). 您会看到,如果ClassC是关于存储播放器的详细信息的……首先将其称为“ Player (即使放置示例时:也使用有意义的名称,以便人们理解您的意图)。

As soon as we talk about players, it becomes clear: a Player object is probably supposed to model a (human or not) player. 我们一谈到玩家,就会很清楚:一个Player对象可能应该建模一个(人类或非人类)玩家。 There are no humans "without" a name or age. 没有名字或年龄的人。 That tells you: wrong approach. 那告诉你:错误的方法。 Your classes and models should reflect reality. 您的类和模型应该反映现实。

You don't create empty object upfront; 您不会在先创建对象; you create them at that point in time when it makes sense (and then you provide the necessary information to them, so you can ideally make the objects immutable, by declaring all your fields to be final). 您可以在有意义的时间点创建它们(然后向它们提供必要的信息,因此理想情况下,您可以通过声明所有字段为最终字段来使对象不可变)。

So, long story short: step back and rethink your design. 因此,长话短说:退后一步,重新考虑您的设计。 If there is "something" to do that doesn't require the presence of a populated Player object, then that should probably be static method and it should probably not sit on the Player class. 如果要执行“某些操作”不需要存在已填充的Player对象,则该对象可能应该是静态方法并且可能应该放在Player类上。

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

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