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

 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 }

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/10458867.html