我不太确定该怎么称呼它,但本质上,当我运行此代码时:
public class test {
static Device one;
static Device two;
public static void main(String[] args) throws Exception {
one = new Device("One", "ONE");
System.out.println(one.getName());
two = new Device("Two", "TWO");
System.out.println(one.getName());
System.out.println(two.getName());
}
}
输出是:
ONE
TWO
TWO
什么时候应该:
ONE
ONE
TWO
设备对象非常简单,它只接收两个字符串,第二个是我要求它打印的“名称”。我以前做过OOP,但我觉得我只是忘记了一些重要的方面,但似乎无法弄清楚。任何帮助表示赞赏,谢谢!
这是设备构造函数:
public Device(String iP, String Name) {
//Set the IP address
IP = iP;
//Set the device's name
name = Name;
// Set the string version of the device (for transmitting)
stringVersion = IP + ";" + name;
}