A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孟庆波 中级黑马   /  2012-4-22 11:21  /  1809 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

多态:通过继承实现的不同对象调用相同的方法,表现出不同的行为,称之为多态
  class Program
    {

        static void Main(string[] args)
        {
            //Person p = new Person();
            //p.Show();
            //p.Height();
            //Console.ReadKey();
            Animal[] animal = new Animal[3];
            animal[0] = new Animal();
            animal[1] = new Cat();
            animal[2] = new Dog();
            for (int i = 0; i < 3; i++)
            {
                animal[i].Eat();
            }
        }
}

   public class Animal
    {
        public virtual void Eat()
        {
            Console.WriteLine("动物叫");
        }

    }

  public class Cat : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("猫叫");
        }
    }
    public class Dog : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("狗叫");
        }
    }

结果为:动物叫
                猫叫   
                狗叫
在上面的例子中,通过继承,使得Animal对象数组中的不同的对象,在调用Eat()方法时,表现出了不同的行为。

1.new的用法
  class Program
    {

        static void Main(string[] args)
        {

            Animal animal = new Animal();
            animal.Eat();
            Animal ac = new Car();
            ac.Eat();
            Car c = new Car();
            c.Eat();
            Console.ReadKey();
        }

}
public class Animal
    {
        public virtual void Eat()
        {
            Console.WriteLine("动物叫");
        }

    }

  public class Car : Animal
    {
        public new void Eat()
        {
            Console.WriteLine("汽车叫");
        }
}
结果为:动物叫
             动物叫
             汽车叫
可以看出,当派生类Cat的Eat()方法使用new修饰时,Car的对象转换为Animal对象后,调用的是Animal类中的Eat()方法。其实可以理解为,使用new关键字后,使得Car中的Eat()方法和Animal中的Eat()方法成为毫不相关的两个方法,只是它们的名字碰巧相同而已。所以, Animal类中的Eat()方法不管用还是不用virtual修饰,也不管访问权限如何,或者是没有,都不会对Car的Eat()方法产生什么影响(只是因为使用了new关键字,如果Car类没用从Animal类继承Eat()方法,编译器会输出警告)。

2.override实现多态

  class Program
    {

        static void Main(string[] args)
        {
            Animal[] animals = new Animal[3];
            animals[0] = new Animal();
            animals[1] = new Dog();
            animals[2] = new WolfDog();
            for (int i = 0; i < 3; i++)
            {
                animals[i].Eat();
            }
            Console.ReadKey();
        }

    }

    public class Animal
    {
        public virtual void Eat()
        {
            Console.WriteLine("动物叫");
        }

    }

public class Dog : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("狗叫");
        }
    }
    public class WolfDog : Dog
    {
        public override void Eat()
        {
            Console.WriteLine("狼狗叫");
        }
    }
结果为:动物叫
             狗叫   
             狼狗叫
在上面的例子中类Dog继承自类Animal,对方法Eat()进行了重写,类WolfDog又继承自Dog,再一次对Eat()方法进行了重写,并很好地实现了多态。不管继承了多少层,都可以在子类中对父类中已经重写的方法继续进行重写,即如果父类方法用override修饰,如果子类继承了该方法,也可以用override修饰,多层继承中的多态就是这样实现的。要想终止这种重写,只需重写方法时用sealed关键字进行修饰即可
http://www.cnblogs.com/charley_yang/archive/2010/09/11/1824025.html
3. abstract-override实现多态
    class Program
    {

        static void Main(string[] args)
        {
            Animal[] animals = new Animal[3];
            animals[0] = new Cat();
            animals[1] = new Dog();
            animals[2] = new WolfDog();
            for (int i = 0; i < 3; i++)
            {
                animals[i].Eat();
            }
            Console.ReadKey();
        }

    }

   public abstract class Animal
    {
        public abstract void Eat();
    }
public class Cat : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("猫叫");
        }
    }
    public class Dog : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("狗叫");
        }
    }
    public class WolfDog : Dog
    {
        public override void Eat()
        {
            Console.WriteLine("狼狗叫");
        }
    }

结果为:猫叫
            狗叫
            狼狗叫
从上面可以看出,通过使用abstract-override可以和virtual-override一样地实现多态,包括多层继承也是一样的。不同之处在于,包含虚拟方法的类可以被实例化,而包含抽象方法的类不能被实例化

评分

参与人数 1技术分 +3 收起 理由
宋天琪 + 3

查看全部评分

2 个回复

倒序浏览
写的哈啊
回复 使用道具 举报
不是很明白
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马