java构造方法重载

 1 class Person
 2 {
 3     private int id;
 4     private int age=20;
 5     Person()
 6     {
 7         id=0;
 8         age=20;
 9     }
10     Person(int _id)
11     {
12         id=_id;
13         age=23;
14     }
15     Person(int _id,int _age)
16     {
17         id=_id;
18         age=_age;
19     }
20     public int getAge()
21     {
22         return age;
23     }
24     public int getId()
25     {
26         return id;
27     }
28     public void setAge(int i)
29     {
30         age=i;
31     }
32     void info()
33     {
34         System.out.println(id);
35     }
36     void info(String t)
37     {
38         System.out.println(t+" "+id);
39     }
40 }
41 
42 
43 
44 public class TestOverLoad {
45     public static void main(String[] args) {
46         Person p=new Person();
47         Person p1=new Person(400);
48         Person p2=new Person(2,500);
49         p.info();
50         p.info("ok");
51         
52     }
53 
54 }

 

posted @ 2013-03-28 10:33  glt66  阅读(174)  评论(0编辑  收藏  举报