简体   繁体   English

适当的构造函数以Java运行所有方法

[英]Proper Constructors to Run all Methods in Java

I have this class and need to know which constructor is needed to create an object that may immediately use all its methods without error 我有这个类,需要知道需要哪个构造函数来创建可以立即使用其所有方法而不会出错的对象

public class Robot {
    private boolean fuelEmpty = true;
    private int roboID;
    private String greeting;
    private String securityProtocol;

//insert robot constructor here

public void destroyAllHumans(){
    while (fuelEmpty == false) {
        //robot begins to destroy all humans
    }
}
public int getRoboID(){
    return roboID;
}
public void greet(){
    System.out.println(greeting);
}
public void setSecurityProtocol(String proto){
    securityProtocol = proto;
}
}

For example should look like this: 例如应如下所示:

public Robot(int id, String greet) {
    roboID = id;
    greeting = greet;
}

or this: 或这个:

public Robot(int id, String greet) {
    roboID = id;
    greeting = greet;
    fuelEmpty = false;
}

or: 要么:

public Robot(boolean full, int id, String greet, String proto) {
    roboID = id;
    greeting = greet;
    fuelEmpty = full;
    securityProtocol = proto;
}

Which of these (or something else different) is needed so that all the other methods can run without an error? 为了使所有其他方法都可以正常运行,需要使用其中哪些(或其他不同方法)?

You can overload the constructor as much as you need, the important thing is the object gets properly instantiated after you create a new one... 您可以根据需要尽可能多地重载构造函数,重要的是创建新对象后,该对象将被正确实例化。

a way can be: 一种方法可以是:

public Robot() {
    this(false, 0, "", "");
}

public Robot(int id) {
    this(false, id, "", "");
}

public Robot(boolean fuelEmpty, int roboID, String greeting, String securityProtocol) {
    this.fuelEmpty = fuelEmpty;
    this.roboID = roboID;
    this.greeting = greeting;
    this.securityProtocol = securityProtocol;
}

so look how all other constructors will at the end call internally the 因此,请看所有其他构造函数在最后如何内部调用

public Robot(boolean fuelEmpty, int roboID, String greeting, String securityProtocol) 

that will give you the waranty that no matter which constructor is invoked, the Robot is fully created and can invoke all those methods without crashing 这将给您保证,无论调用哪个构造函数,机器人都会完全创建,并且可以调用所有这些方法而不会崩溃

The solution works like this: 该解决方案的工作原理如下:

  • you look at each of your methods 你看你的每种方法
  • you check which fields each method is using 您检查每种方法使用的字段
  • you check more closely, if the method breaks when that field has its default value (like null for Objects, or false for booleans) 如果该字段具有其默认值时该方法是否中断(例如,对于Objects为null ,对于布尔false ),则您将更仔细地检查

When you do that for all methods, you get a list of those fields that you need to initialize somehow. 当对所有方法都执行此操作时,您将获得需要以某种方式初始化的那些字段的列表。 Then you could go forward and define a corresponding constructor. 然后,您可以继续定义一个相应的构造函数。

But of course, that is the wrong approach. 但是,当然,这是错误的方法。

The real answer goes like this: you don't put fields into a class because you can . 真正的答案是这样的:您不必将字段放入类,因为可以 You add them because they are required so that this class can implement the requirements (responsibilities) that you want it to implement. 您添加它们是因为它们是必需的,以便此类可以实现您希望其实现的要求(职责)。 Meaning: you focus on the methods that your class should provide. 含义:您专注于类应提供的方法。 Then you clarify which fields you need in order to implement these methods. 然后,您需要确定实现这些方法所需的字段。

In other words: you have exactly those fields in your class that your class needs. 换句话说:您的班级中确实有班级需要的那些字段。 If you have fields in there that go unused - then you get rid of them. 如果您有未使用的字段-那么您将摆脱它们。

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

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