C#函数(构造函数)的重载
程序员文章站
2022-04-02 18:25:39
using System; namespace test { class Program { static void Main(string[] args) { Cat cat = new Cat();//不含参数的构造方法 Console.WriteLine("姓名是{0},年龄是{1}",cat ......
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; } } } }
上一篇: 蜘蛛侠就这么被放到了
下一篇: 啤酒大揭秘:华润雪花啤酒价格是多少