简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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