为什么打印出的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")

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

img

要覆写toString方法,把方法名字tostString改成toString