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; } } } }