java面向对象------- object类及toString方法的重写

时间:2021-10-20 16:15:15
package java面向对象;

/**
* 测试object类
*/
public class TestObject extends Object{

public String toString(){
return "测试重写";
}

public static void main(String[] args) {
TestObject to=new TestObject();
System.out.println(to.toString());

Person2 p2=new Person2("小红",18);
System.out.println(p2.toString());

}

}

class Person2{
String name;
int age;
public String toString(){
return name+"年龄"+age;
}
public Person2(String name,int age){
this.name=name;
this.age=age;
}

}