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();
    }
}
复制代码

打印:

This is Father
This is son
This is daughter

 

复制代码
/*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();
        }
    }
}
复制代码

打印内容:

drink ...
playGame ...
dance ...
drink ...
drink ...
drink ...

 

posted on   Hello-World3  阅读(143)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示