polymorphism,是多态性。。,认真看就明白,有C++基础就更明白了。

解决方案 »

  1.   

    class Shape {
      void draw() {System.out.println("ShapeDraw");}
      void show()
      {
        System.out.println("ShapeShow");
        draw();
      }
    }
    class Circle extends Shape {
      void draw() {    //这个方法把超类的方法覆盖了,造成死循环
        System.out.println("CircleDraw");
        super.show();
        System.out.println("CircleShow");
      }
    }public class Leaf {
      public static void main(String[] args) {
        Circle c=new Circle();
        c.draw();
      }
    }
    这题最后是个死循环,因为子类把超类的draw()方法覆盖了~!
    不知道这个例子给你看看有没有用~!~!
      

  2.   

    看看这个例子:
    class animal{
      public void walk(){
         System.out.println("animal walk()");
      }
    }
    class dog extends animal{
      public void walk(){
        System.out.println("dog walk()");
      }
    }class cat extends animal{
    public void walk(){
    System.out.println("cat walk()");
    }
    }public class TestAnimal{
    public static void main(String[] args){
    animal a=new animal();
    animal b=new dog();
    animal c=new cat();
    a.walk();
    b.walk();
    c.walk();
    }
    }
      

  3.   


    多态性涉及到JAVA的动态方法调用,只有在产生继承(或间接继承)的方法之间才会产生多态性。
    class A{
        void method1(){
           System.out.println("method1 in the A class");
        }
        void method2(){
           System.out.println("method2 in the A class");
        }
    }
    class B extends A{
        void method1(){
           System.out.println("method1 in the B class");
        }
        void method3(){
           System.out.println("method3 in the B class");
        }
    }
    public class C{
        public static void main(String[] args){       //发生多态,变量a的形式类型是A,但实际类型是B(一定要记住,因为对象的值是个引用地址,不管a前面的类型是什么,它指向的永远都是类B的引用地址)
           A a = new B(); 
           a.method1(); //调用类B的方法:method1
           a.method2(); //调用类A的方法:method2,因为B通过继承也得到了method2
           a.method3(); //编译错误,因为编译器未找到method3,从这点可以看出, 形式类型属于编译期类型,而实际类型属于运行期类型。
           }
    }