重写toString方法
程序员文章站
2022-05-23 23:13:43
...
package test;
public class TestObject {
public static void main(String[] args) {
//Object obj;
TestObject to = new TestObject();
System.out.println(to.toString());
Person2 p2 = new Person2("西西", 6);
System.out.println(p2.toString());
}
public String toString() {
return "测试Obect对象";
}
}
class Person2{
String name;
int age;
public String toString() {
return name+",年龄:"+age;
}
//重写构造器
public Person2(String name,int age) {
this.name = name;
this.age = age;
}
}
测试Obect对象
西西,年龄:6
下一篇: 5步详解PHP文件下传功能的实现