首页 > 代码库 > 重写虚方法,多态的使用

重写虚方法,多态的使用

重写(override)特点:
1、要有继承关系
2、方法原型要一致(修饰符、返回类型、方法名、参数列表一致)
3、加上override关键字,重写父类的方法(该父类方法要有virtual、abstract进行修饰)

 

  /// <summary>
    /// 狗类(父类)
    /// </summary>
    class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Color { get; set; }

    //定义虚方法
        public virtual void Run()
        {
            Console.WriteLine("速度:一般");
        }

        //重写Object基类的ToString()方法
        public override string ToString()
        {
            return "名称:" + this.Name + " 年龄:" + this.Age + " 毛色:" + this.Color;
        }

        //重写Object的Equals方法(注意:Object的Equals是比较对象的引用是否相等,本方法重写后是比较对象里面的属性值是否全部相等)
        public override bool Equals(object obj)
        {
            Dog dog = obj as Dog;
            if (dog.Name == this.Name && dog.Age == this.Age && dog.Color == this.Color)
            {
                return true;
            }
            return false;
        }
    }

 

/// <summary>
    /// 格力犬
    /// </summary>
    class GLDog:Dog
    {

    //重写虚方法
        public override void Run()
        {
            Console.WriteLine("速度:最快");
        }
    }

 /// <summary>
    /// 猎狗类
    /// </summary>
    class LGDog:Dog
    {
        public override void Run()
        {
            Console.WriteLine("速度:快");
        }

        public void Method()
        {
            Console.WriteLine("aaa");
        }
    }

 

/// <summary>
    /// 狮毛狗
    /// </summary>
    class SMDog:Dog
    {

        //由于狮毛狗速度一般,所以直接使用父类的Run方法即可
    }

 

//使用

class Program
    {
        //方法的普通使用
        //static void Main(string[] args)
        //{
        //    Dog dog1 = new Dog();
        //    dog1.Run();
        //    SMDog dog2 = new SMDog();
        //    dog2.Run();
        //    LGDog dog3 = new LGDog();
        //    dog3.Run();
        //    GLDog dog4 = new GLDog();
        //    dog4.Run();
        //}

        //多态的使用1(特点:在多态形式下调用重写方法时,不会执行父类被重写的方法,而会执行子类中重写的方法)
        //static void Main(string[] args)
        //{
        //    //多态的表现形式(父类引用指向子类对象,即子类对象存入父类变量)
        //    Dog dog = new LGDog();
        //    dog.Run();//输出  速度:快
        //    dog = new GLDog();
        //    dog.Run();//输出  速度:最快
        //}

        //多态的使用2
        //static void Main(string[] args)
        //{
        //    Dog[] dogs = new Dog[3];
        //    SMDog sm = new SMDog();
        //    LGDog lg = new LGDog();
        //    GLDog gl = new GLDog();
        //    dogs[0] = sm;
        //    dogs[1] = lg;
        //    dogs[2] = gl;
        //    foreach (Dog dog in dogs)
        //    {
        //        dog.Run();
        //    }
        //}

        //多态的使用3
        //static void Main(string[] args)
        //{
        //    SMDog dog1 = new SMDog();
        //    Method(dog1);
        //    LGDog dog2 = new LGDog();
        //    Method(dog2);
        //    GLDog dog3 = new GLDog();
        //    Method(dog3);
        //}
        //public static void Method(Dog dog)
        //{
        //    dog.Run();
        //}

        //重写ToString()测试
        //static void Main(string[] args)
        //{
        //    Dog dog = new Dog() {Name="小黄",Age=2,Color="黄色" };
        //    Console.WriteLine(dog.ToString());
        //}

        //Equals重写测试
        //static void Main(string[] args)
        //{
        //    Dog dog1 = new Dog() { Name = "小白", Age = 2, Color = "白色" };
        //    Dog dog2 = new Dog() { Name = "小白", Age = 2, Color = "白色" };
        //    //Dog dog2 = dog1;
        //    Console.WriteLine(dog1.Equals(dog2));
        //}

        //了解GetType方法
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Console.WriteLine("公共方法如下:");//公共方法不包含构造方法
            MethodInfo[] methods = dog.GetType().GetMethods();
            foreach (MethodInfo item in methods)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("成员如下:");
            MemberInfo[] members = dog.GetType().GetMembers();
            foreach (MemberInfo item in members)
            {
                Console.WriteLine(item.Name);
            }
        }
    }