public class ex11_2 {
public static void main(String[] args){
Object a=new Person("abc1","def1","ghi1");
System.out.println(a.toString());
Object b=new Person("abc2","def2","ghi2");
System.out.println(a.toString());
}
}
class Person{
private static String name;
private static String address;
private static String phoneNo;
public Person(){
}
public Person(String name,String address,String phoneNo){
setName(name);
setAddress(address);
setPhoneNo(phoneNo);
}
public String toString(){
return super.toString()+(" "+getName()+","+getAddress()+","+getPhoneNo()+".");
}
public static String getName() {
return Person.name;
}
public static void setName(String name) {
Person.name = name;
}
public static String getAddress() {
return address;
}
public static void setAddress(String address) {
Person.address = address;
}
public static String getPhoneNo() {
return phoneNo;
}
public static void setPhoneNo(String phoneNo) {
Person.phoneNo = phoneNo;
}
}
大家好。我是Java编程的初学者,想问一个关于OOP的问题。在这个小程序中,我创建了一个 Person 类并测试它的 toString() 方法。
测试结果是:
Person@635b9e68 abc1,def1,ghi1.
Person@635b9e68 abc2,def2,ghi2.
我想知道为什么 b 的数据字段会在创建 b 之后替换 a 的数据字段。谢谢