本帖最后由 贺奕凯 于 2013-10-28 23:24 编辑
- <p>class Person
- {
- private int age;
- private String name;
-
- //不同的构造函数(重构)
- Person()
- {
- System.out.println("A: name="+name+",,age="+age);
- }
- Person(String name)
- {
- this();
- this.name = name;
- System.out.println("B: name="+name+",,age="+age);
- }
- Person(String name,int age)
- {
- //构造函数不能通过Person();来互相调用,而要同过以下方式
- this(name);
- this.age = age;
- System.out.println("C: name="+name+",,age="+age);
- }
- }</p><p>public class Lei {</p><p> public static void main(String[] args) {
- Person p1=new Person();
- Person p2=new Person("zhang");
- Person p3=new Person("zhang",20);
- </p><p> }</p><p>}</p>
复制代码 看得是毕老师的视频,跟着写了写,加了printfln语句测试下输出,发现输出结果为:- A: name=null,,age=0
- A: name=null,,age=0
- B: name=zhang,,age=0
- A: name=null,,age=0
- B: name=zhang,,age=0
- C: name=zhang,,age=20
复制代码 没太听懂{:soso_e150:}
|