首页 > 其他 > 详细

System.out.println(对象)

时间:2014-10-09 12:41:33      阅读:131      评论:0      收藏:0      [点我收藏+]
 1 class Person {
 2     private String name;
 3     private int age;
 4     public String getName() {
 5         return this.name;
 6     }
 7     
 8     public void setName(String name) {
 9         this.name = name;
10     }
11     
12     public int getAge() {
13         return this.age;
14     }
15     
16     public void setAge(int age) {
17         this.age = age;
18     }
19     
20     public int getAge() {
21         return this.age;
22     }
23     
24     public String toString() {
25         return "姓名: " + this.name + ", 年龄: " + this.age;
26     }
27 }
28 
29 public class InstanceDemo01 {
30     public static void main(String args[]) {
31         Class<?> c = null;
32         try {
33             c = Class.forName("Person");
34         } catch(ClassNotFoundException e) {
35             e.printStackTrace();
36         }
37         
38         Person per = null;
39         try {
40             per = (Person)c.newInstance();    // 实例化Person对象
41         } catch(Exception e) {
42             e.printStackTrace();
43         }
44         
45         per.setName("李兴华");
46         per.setAge(30);
47         System.out.println(per);            // 内容输出: 调用toString()
48     }
49 }

 

System.out.println(对象)

原文:http://www.cnblogs.com/daishuguang/p/4012445.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!