2016.7.12 多态的解析

/*

类中的多态:

1.多态的体现

父类的引用指向子类。

2多态的前提:

(1)子父类一定要存在着关系,是继承或者是实现。

(2)必须得有重写。

3多态的特点:

(1)编译过程:参阅的引用变量所属的类中必须得有方法。

(2)运行过程;参阅的变量所属的类中必须有个与引用变量类中方法的重写方法;不然就直接调用引用变量中的,也会使得多态有个毛线用啊!

4多态的弊端:

虽然大大提高了代码的可扩展性,但是只能调用父类中的引用,父类的成员

*/

class Animal{

  void sing(){

    System.out.println("唱歌");

}

}

class Cat extends Animal(){

  void sing(){

    System.out.println("喵喵地唱歌");

}

  void catchmouse(){

    System.out.pringln("奋力地捕鼠");

}

}

class Dog extends Animal(){{

  void sing(){

  Sytsem.out.println("汪汪地唱歌");

  }

}

public class text22{

  public static void main(String[] args){

    Cat c=new Cat();

    c.funcition();

    Dog d=new Dog();

    d.funcition();

    public static void funcition(Animal a){//向上转型

       a.sing();

    }

}

}

posted @ 2016-07-12 21:38  对与错  阅读(98)  评论(0编辑  收藏  举报