liuboris.slan 2021-10-05 18:17 采纳率: 100%
浏览 33
已结题

为什么打印出的p1,p2是他们的虚拟地址,不是具体的值?


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")

  • 写回答

2条回答 默认 最新

  • Pliosauroidea 2021-10-05 18:21
    关注

    你打印的p1是一个对象,对象默认的tostring方法就是打印他所在的地址
    你tostring写错了

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月13日
  • 已采纳回答 10月5日
  • 创建了问题 10月5日

悬赏问题

  • ¥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之后自动重连失效