using System; namespace test { class Program { static void Main(string[] args) { Cat cat = new Cat();//不含参数的构造方法 Console.WriteLine("姓名是{0},年龄是{1}",cat.name,cat.age); Cat cat1 = new Cat("一只猫");//含1个参数的构造方法 Console.WriteLine("姓名是{0},年龄是{1}", cat1.name, cat1.age); Cat cat2 = new Cat("又一只猫",18);//含2个参数的构造方法 Console.WriteLine("姓名是{0},年龄是{1}", cat2.name, cat2.age); } class Cat { public string name; public int age; public Cat() { } public Cat(string nameValue) { name = nameValue; } public Cat(string nameValue, int ageValue) { name = nameValue; age = ageValue; } } } }
using System; namespace test { class Program { static void Main(string[] args) { int c = Calculate.Divide(7, 3); Console.WriteLine(c); double d = Calculate.Divide(7, 3.0); Console.WriteLine(d); } /// <summary> /// 方法名称一样 参数类型不一样 构成重载 /// </summary> class Calculate { public static int Divide(int a,int b) { return a / b; } public static double Divide(double a, double b) { return a / b; } } } }