System.out.println(对象)

 class Person {
private String name;
private int age;
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
} public int getAge() {
return this.age;
} public String toString() {
return "姓名: " + this.name + ", 年龄: " + this.age;
}
} public class InstanceDemo01 {
public static void main(String args[]) {
Class<?> c = null;
try {
c = Class.forName("Person");
} catch(ClassNotFoundException e) {
e.printStackTrace();
} Person per = null;
try {
per = (Person)c.newInstance(); // 实例化Person对象
} catch(Exception e) {
e.printStackTrace();
} per.setName("李兴华");
per.setAge(30);
System.out.println(per); // 内容输出: 调用toString()
}
}
上一篇:JS里面的懒加载(lazyload)


下一篇:POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)