using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { cat c = new cat("萌萌");//先执行cat(string s,int i)构造函数 Console.WriteLine("名字:{0},年龄:{1}", c.name, c.age);//输出名字:萌萌,年龄:1 Console.ReadKey(); } } class cat { public string name; public int age; public cat(string s,int i) { name = s; age = i; } public cat(string s) : this("小萌", 1) //构造函数初始化器(匹配参数相同的构造函数)【使用基类的构造函数则用关键字base】 { name = s; } } }
时间: 2024-10-13 22:26:47