Java中的多态

一、总结

1.多态:当子类对象赋值给父类对象后,通过父类对象调用成员方法,恰好子类重写了这个成员方法,此时通过父类调用到的是子类的成员方法(若没重写则是继承父类的方法)。

2.向上转换的时候(Father的对象=Son的对象)不需要加强制类型转换,也就是说子类对象可以直接赋值给父类对象。
但是父类对象不能直接赋值给子类对象,向下转换(Son的对象=Father的对象)的时候就需要加强制类型转换,并且前提是Father对象来源于Son对象,也即是如下关系。当有多个子对象时需要借助instanceof来判断实例来源于哪个类。
  Father f = new Son();
  Son s = (Son)f; /*约等效于:Son s = new Son()*/

3.instanceof关键字:用来判断一个对象是不是某个类的实例。
注意:所有的子类的对象都可以认为是父类的实例。所有的子类对象在判断 (子类对象实例 instanceof 父类类型) 的时候都是为真的。

4.Object类是所有类的父类,它可以做左值接收任何对象,但是不一定能表现出多态,因为Object中可能根本没有实现你要多态的函数。

5.当你重写一个继承来的成员方法时,访问权限不能更weaker(eg:把继承来的protected方法变为private), 但是可以更Stronger,例如把protected方法重写为public方法。

 

二、试验Demo

/*Cnv4.java: Test Father=Son*/

class Father {
    private int money;    

    public int getMoney() {return money; }
    public void setMoney(int money) {this.money = money; }

    public void printInfo() {System.out.println("This is Father");}
}

class Son extends Father{
    public void printInfo() {System.out.println("This is son");}
    public void playGame() {System.out.println("playGame ...");} 
}

class Daughter extends Father{
    public void printInfo() {System.out.println("This is daughter");}
}

public class Cnv4 {
    public static void main (String args[]) {
        Father f = new Father();
        Son s = new Son();
        Daughter d = new Daughter();

        print(f);
        print(s);
        print(d);
    }

    public static void print(Father f) {
        f.printInfo();
    }
}

 

/*Cnv5.java: Test Son=Father in printAction()*/

class Father {
    private int money;    

    public int getMoney() {return money; }
    public void setMoney(int money) {this.money = money; }

    public void printInfo() {System.out.println("This is Father");}
    public void drink() {System.out.println("drink ...");}
}

class Son extends Father{
    public void printInfo() {System.out.println("This is son");}
    public void playGame() {System.out.println("playGame ...");}   
}

class Daughter extends Father{
    public void printInfo() {System.out.println("This is daughter");}
    public void dance() {System.out.println("dance ...");}
}

public class Cnv5 {
    public static void main (String args[]) {
        Father f = new Father();
        Son s = new Son();
        Daughter d = new Daughter();

        printAction(f);
        printAction(s);
        printAction(d);

        printAction_2(f);
        printAction_2(s);
        printAction_2(d);
    }

    public static void printAction(Father f) {  /*Son --> Father --> Son*/
        if (f instanceof Son) {
            Son son = (Son)f;
            son.playGame();
        }
        else if (f instanceof Daughter) {
            Daughter d = (Daughter)f;
            d.dance();
        }
        else if (f instanceof Father) {
            f.drink();
        }    
    }

    public static void printAction_2(Father f) {  /*Son --> Father --> Son*/
        if (f instanceof Father) {
            f.drink();
        }    
        else if (f instanceof Son) {
            Son son = (Son)f;
            son.playGame();
        }
        else if (f instanceof Daughter) {
            Daughter d = (Daughter)f;
            d.dance();
        }
    }
}

 

posted on 2019-02-24 11:08  Hello-World3  阅读(143)  评论(0编辑  收藏  举报

导航