可以把整个的代码给我发一下吗?

来源:2-3 虚方法和多态

慕容3978549

2017-12-28 19:40

虚方法和多态的代码

写回答 关注

1回答

  • Sneakerhead
    2017-12-30 16:21:16
    已采纳

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
        //public class Pet {
        //    public Pet(string name) {
        //        _name = name;
            
        //    }
        //    protected string _name;
        //    public void PrintName() {

        //        Console.WriteLine("Pet`s name is"+_name);  
        //    }
        //   //虚方法
        //    virtual public void speak() {

        //        Console.WriteLine(_name+"is speaking");
        //    }
        //}
        //结构
        struct fish {
            int weight;
            int price;
        
        }
        //接口
        interface ICatchMice {

            void catchMice();
        }
        interface IClimbTree {

            void climbTree();
        }
       abstract public class Pet
        {
            public Pet(string name)
            {
                _name = name;

            }
            protected string _name;
            public void PrintName()
            {

                Console.WriteLine("Pet`s name is" + _name);
            }
            //抽象类,没有函数体,无法实例化
            abstract public void speak();
            //{

                //Console.WriteLine(_name + "is speaking");
            //}
        }
        //狗类
        public class Dog : Pet {
            //静态
            static int num;
            static Dog() {
                num = 0;
            
            }
            //构造函数
            public Dog(string name):base(name) {
                //_name = name;
                ++num;
            
            }
            new public void PrintName() {
                Console.WriteLine("我的宠物是"+_name);
            }
            public override void speak()//重写
            {
                Console.WriteLine(_name + " is speaking"+" wow");
            }
            static public void ShowNum() {

                Console.WriteLine("有"+num+"条狗");
            }
        }
        //猫类
        public class Cat : Pet,ICatchMice,IClimbTree {
            //构造函数
            public Cat(string name):base(name) {
                //_name = name;
            
            }
            new public void PrintName() {
                Console.WriteLine("我的宠物是"+_name);
            }
            public override void speak()
            {
                Console.WriteLine(_name + "is speaking"+" meow");
            }
            public void catchMice() {

                Console.WriteLine("catchmice");
            }
            public void climbTree() {
                Console.WriteLine("climbtree");
            }
        
        }
        //静态类,方法扩展
        static class PetGuide{

            static public void HowToFeed(this Dog dog) {
                Console.WriteLine("Play a vedio how to feed dog");
            }
        
        }
        class Program
        {
            static void Main(string[] args)
            {

                //Dog Dog = new Dog();
                //Dog.Name = "Jack";
                //Dog.PrintName();
                //Dog.speak();
                //Pet Cat = new Cat();
                //Cat.Name = "Tom";
                //Cat.PrintName();
                //Cat.speak();
                Pet[] pets = new Pet[]{new Dog("Jack"),new Cat("Tom")};
                for (int i = 0; i < pets.Length;i++ ) {
                    pets[i].speak();
                 
                }
                Cat c = new Cat("Tom2");
                IClimbTree climb = (IClimbTree)c;
                c.catchMice();
                climb.climbTree();
                Dog a = new Dog("Jack1");
                Dog.ShowNum();

                Dog dog = new Dog("Tommy");
                dog.HowToFeed();



            }
        }
    }

    慕容3978...

    非常感谢!

    2018-01-02 18:10:28

    共 1 条回复 >

C#面向对象编程

本系列教程主要是,带你学习C#面向对象编程的编程思想、编程技巧

68612 学习 · 153 问题

查看课程

相似问题