public class Person {
public String name;
public MyDate birthdate;
public String gender, province, city;
private static int count = 0;
// 构造方法
public Person(String name, MyDate birthdate, String gender, String province, String city) {
this.set(name, birthdate, gender, province, city);
count++;
}
public Person(String name, MyDate birthdate) {
this(name, birthdate, "", "", "");
}
public Person() {
this("", new MyDate());
}
public Person(Person per) {
this(per.name, new MyDate(per.birthdate), per.gender, per.province, per.city);
}
public void finalize() {
System.out.println("释放对象(" + this.toString() + ")");
Person.count--;
}
public static void howMany() {
System.out.println(Person.count + "个Person对象,");
}
private void set(String name, MyDate birthdate, String gender, String province, String city) {
// TODO Auto-generated method stub
this.name = name == null ? "" : name;
this.birthdate = birthdate;
this.gender = gender == null ? "" : gender;
this.province = province == null ? "" : province;
this.city = city == null ? "" : city;
}
public void set(String name, MyDate birthdate) {
this.set(name, birthdate, "", "", "");
}
public String tostString() {
return this.name + "," + (this.birthdate == null ? "" : birthdate.toString()) + "," + this.gender + ","
+ this.province + "," + this.city;
}
public static void main(String[] args) {
Person p1 = new Person("李小明",new MyDate(1994,3,15));
Person p2 = new Person(p1);
Person.howMany();
System.out.println("p1: " + p1 + " ; p2: " + p2 + "\np1==p2? " + "; p1.name==p2.name? " + (p1.name == p2.name)
+ ", p1.birthdate==p2.birthdate? " + (p1.birthdate == p2.birthdate));
p2.name = "张" + p2.name.substring(1);
// MyDate date = p2.birthdate;
// date.set(date.getYear() + 2, date.getMonth(), date.getDay());
// System.out.println("p1: " + p1 + "; p2: " + p2);
p1.finalize();
Person.howMany();
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/987059824336135.png "=600 #left")
为什么打印出的p1,p2是他们的虚拟地址,不是具体的值?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- Pliosauroidea 2021-10-05 18:21关注
你打印的p1是一个对象,对象默认的tostring方法就是打印他所在的地址
你tostring写错了本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效