Пример #1
0
        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();
        }
Пример #3
0
        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();
        }
Пример #4
0
        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();
        }