构造方法调用另一个构造方法,用this

Posted on 2019-03-01 21:28  金色的省略号  阅读(200)  评论(0编辑  收藏  举报
 1 using System;
 2 class Person
 3 {
 4     public int age;
 5     public string name;
 6     public Person(int age, string name){
 7         this.age = age;
 8         this.name = name;
 9     }
10     /*构造方法调用另一个构造方法,使用this*/
11     public Person():this(0,""){}
12 }
13 public class Test
14 {
15     public static void Main( string [] args )
16     {
17        Person p = new Person();
18        Console.WriteLine(p.age);
19        Person p1 = new Person(22,"Li Li");
20        Console.WriteLine(p1.name);
21     }
22 }