static void Main(string[] args) { Speaker speaker = new Speaker(); IChineseGreeting iChineseGreeting = (IChineseGreeting)speaker; iChineseGreeting.Greeting(); IAmericanGreeting iAmericanGreeting = (IAmericanGreeting)speaker; iAmericanGreeting.Greeting(); Console.Read(); }
// 隐式接口实现存在的问题 static void Main(string[] args) { // 初始化类实例 Speaker speaker = new Speaker(); // 调用中国人打招呼方法 IChineseGreeting iChineseG = (IChineseGreeting)speaker; iChineseG.SayHello(); // 调用美国人招呼方法 IAmericanGreeting iAmericanG = (IAmericanGreeting)speaker; iAmericanG.SayHello(); Console.Read(); }
static void Main(string[] args) { /* * 调用接口中的方法 * 前面介绍了如何继承一个接口并且实现其中的方法, * 接下来看如何去调用接口中的方法,具体调用代码如下: * **/ Person p1 = new Person(); Person p2 = new Person(); p1.Age = 20; p2.Age = 30; if (p1.CompareTo(p2) > 0) { Console.WriteLine("P1 greater than p2!"); } else if (p1.CompareTo(p2) < 0) { Console.WriteLine("p1 less than p2!"); } else { Console.WriteLine("p1 equal to p2!"); } Greet greet = new Greet(); ((IChineseGreeting)greet).sayHello(); IChineseGreeting chinese = greet; chinese.sayHello(); IAmericanGreeting american = greet; american.sayHello(); Console.ReadKey(); }
static void Main(string[] args) { //常量 const string TEST = "test"; //定义输出文本变量 string welcomeText = "welcome"; //输出文本到控制台 Console.WriteLine(welcomeText); //结构体 Complex number1 = new Complex(1, 2); Complex number2 = new Complex(3, 4); //用+运算符对两个复数进行相加 如果复数类型中没有对+运算符进行重载 则不能使用+运算符来对复数类型进行相加操作 Complex sum = number1 + number2; Console.WriteLine("first: {0}, {1}", number1.real, number1.imaginary); Console.WriteLine("second: {0}, {1}", number2.real, number2.imaginary); Console.WriteLine("first and second: {0}, {1}", sum.real, sum.imaginary); //Console.Read(); Statements statements = new Statements(); statements.statementsIF(); statements.statementSWITCH(); statements.statementWHILE(); statements.statementDOWHILE(); statements.statementFOR(); statements.statementFOREACH(); Person person = new Person(); person.Print("printString"); person.Print(1); person.Print("printSting", 1); //索引器 person[0] = 1; person[1] = 2; person[3] = 3; Console.WriteLine(person[0]); Console.WriteLine(person[1]); Console.WriteLine(person[2]); //OOP Horse horse = new Horse(); horse.Print(); Sheep sheep = new Sheep(); sheep.Voice(); horse.Voice(); horse.Eat(); ((Animal)horse).Eat(); //调用接口中的方法 PersonImp p1 = new PersonImp(); p1.Age = 18; PersonImp p2 = new PersonImp(); p2.Age = 19; if (p1.CompareTo(p2) > 0) { Console.WriteLine("p1比p2大"); } else if (p1.CompareTo(p2) < 0) { Console.WriteLine("p1比p2小"); } else { Console.WriteLine("p1和p2一样大"); } //隐式接口调用/显式接口调用 Speaker speaker = new Speaker(); //调用中国人打招呼方法 IChineseGreeting iChineseG = (IChineseGreeting)speaker; iChineseG.SayHello(); //调用美国人打招呼方法 IAmericanGreeting iAmericanG = (IAmericanGreeting)speaker; iAmericanG.SayHello(); //让控制台程序可见知道用户按下任意字符位置 Console.ReadKey(); }
static void test() { Person p1 = new Person(); Person p2 = new Person("I'M PANJINLIAN."); Person p3 = Person.GetPerson(); Console.WriteLine(p1.Name); Console.WriteLine(p2.Name); Console.WriteLine(p3.Name); Person p4 = new Person(); p4[0] = "hello"; p4[1] = " "; p4[2] = "world"; Console.WriteLine(p4[0] + " " + p4[2]); Horse hrs1 = new Horse(); hrs1.Age = 2; Horse hrs2 = new Horse(666); hrs2.Age = 3; Animal hrs3 = new Horse(1); hrs3.Voice(); Horse hrs4 = new Horse(5); hrs4.Voice(); //Customer Interface practice. Customer cst1 = new Customer(12); Customer cst2 = new Customer(23); if (cst1.CompareTo(cst2) < 0) { Console.WriteLine("{0}比{1}小。", cst1.Age.ToString(), cst2.Age.ToString()); } else if (cst1.CompareTo(cst2) > 0) { Console.WriteLine("{0}比{1}大。", cst1.Age.ToString(), cst2.Age.ToString()); } else { Console.WriteLine("一样大。"); } //隐式接口不管哪个接口实现的方法都是相同的,但并不是我们期望的结果 //显式 Speaker spk = new Speaker(); IChineseGreeting icg = (IChineseGreeting)spk; icg.SayHello(); IAmericaGreeting iag = (IAmericaGreeting)spk; iag.SayHello(); Console.ReadKey(); }