【发布时间】:2018-02-13 22:29:23
【问题描述】:
我有这个类,需要知道需要哪个构造函数来创建一个可以立即使用其所有方法而不会出错的对象
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;
}
}
例如应该是这样的:
public Robot(int id, String greet) {
roboID = id;
greeting = greet;
}
或者这个:
public Robot(int id, String greet) {
roboID = id;
greeting = greet;
fuelEmpty = false;
}
或:
public Robot(boolean full, int id, String greet, String proto) {
roboID = id;
greeting = greet;
fuelEmpty = full;
securityProtocol = proto;
}
需要这些(或其他不同的东西)中的哪一个,以便所有其他方法可以运行而不会出错?
【问题讨论】:
-
离题:你应该这样做 while (!fuelEmpty ) { 代替
-
由于
destroyAllHumans方法的主体未定义,因此很难判断需要哪些类成员。securityProtocol是干什么用的? -
欢迎来到 Stack Overflow!我注意到你的问题仍然是“开放的”——因为你没有接受答案。请查看并决定是否要accept 回答。或者让我知道我是否可以做些什么来增强我的输入以使其被接受。接受有助于未来的读者确定问题是否已解决,并对花时间回答你的人表示感谢。谢谢!
标签: java class object methods constructor