本人初学Java,现在对于多态和接口还很模糊,感觉很抽象,不好理解。  牛人们当时是怎么理解的啊? 请教一下,有没有什么好的方法来理解啊 ?

解决方案 »

  1.   

    多 态 机 制                                             ┌─-────-─┐
                                          │  Feed │
                                          ├───────┤
                                          │ feed()│
                                          └───┬───┘
                                              │
                           ┌─────——─————————──┴——————————────────┐
                           │                                     │
                           │                                     │
                       ┌───┴───┐                             ┌───┴───┐
                       │  Food │                             │Animal │
                       ├───────┤                             ├───────┤
                       │       │                             │ eat() │
                       └───┬───┘                             └───┬───┘   
                      ┌────┴────┐                           ┌────┴────┐
                      │         │                           │         │
                   ┌──┴──┐   ┌──┴──┐                     ┌──┴──┐   ┌──┴──┐ 
                   │Bone │   │Fish │                     │ Dog │   │Cat  │
                   ├─────┤   ├─────┤                     ├─────┤   ├─────┤
                   │     │   │     │                     │eat()│   │eat()│
                   └─────┘   └─────┘                     └─────┘   └─────┘                  图1-1  饲养员Feeder、食物Food 和 动物Animal 及它的子类的类框图  
    public class Feeder{
        public void feed(Animal animal, Food food){
            animal.eat(food);
        }
    }
    Feeder feeder = new Feeder();
    Animal animal = new Dog();
    Food food = new Bone();
    feeder.feed(animal, food);     //给狗喂肉骨头animal = new Cat();
    food = new Fish();
    feeder.feed(animal, food);     //给猫喂鱼
    Animal animal = new Dog();
    Dog dog = (Dog)animal;         //向下转型,把Animal类型转换为Dog类型 
    Creature creature = animal;    //向上转型,把Animal类型转换为Creature类型
    ──────────────────────────────────────────────────例:父类Base和子类Sub中都定义了 实例变量var、实例方法method()、静态变量staticVar和静态
        方法staticMethod(),此外,在Sub类中还定义了 实例变量subVar 和 subMethod()。package com.kejiangwei.test;class Base{
        String var = "BaseVar";                      //实例变量
        static String staticVar = "StaticBaseVar";   //静态变量
        
        void method(){                               //实例方法
            System.out.println("Base method");
        }    static void staticMethod(){                  //静态方法
            System.out.println("Static Base method");
        }
    }public class Sub extends Base{
        String var = "SubVar";                       //实例变量
        static String staticVar = "StaticSubVar";    //静态变量
        void method(){                               //覆盖父类的method()方法
            System.out.println("Sub method");
        }    static void staticMethod(){                  //隐藏父类staticMethod()方法
            System.out.println("Static Sub method");
        }    String subVar = "Var only belonging to Sub";
        void subMethod(){
            System.out.println("Method only belonging to Sub");
        }    public static void main(String[] args){
            Base who = new Sub();                         //who被声明为Base类型,引用Sub实例 
            System.out.println("who.var = " + who.var);   //打印Base类的var变量
            System.out.println("who.staticVar = " + who.staticVar);  //打印Base类的staticVar变量
            who.method();                                 //打印Sub实例的method()方法  
            who.staticMethod();                           //打印Base类的staticMethod()方法
        }
    }
      

  2.   

    刚学java这些东西没必要死扣.