摘要:中的多態(tài)引用的多態(tài)父類的引用是可以指向子類對象報錯方法多態(tài)動物具有吃的能力狗具有吃肉的能力狗具有看門的能力父類的引用是可以指向子類對象報錯
java中的多態(tài) 引用的多態(tài)
/javaDemo4/src/com/imooc/Animal.java
package com.imooc; public class Animal { }
/javaDemo4/src/com/imooc/Dog.java
package com.imooc; public class Dog extends Animal { }
/javaDemo4/src/com/imooc/Initail.java
package com.imooc; public class Initail { public static void main(String[] args) { Animal obj1 = new Animal(); Animal obj2 = new Dog(); //父類的引用是可以指向子類對象 //Dog obj3 = new Animal(); //報錯 } }方法多態(tài)
/javaDemo4/src/com/imooc/Animal.java
package com.imooc; public class Animal { public void eat() { System.out.print("動物具有吃的能力"); } }
/javaDemo4/src/com/imooc/Dog.java
package com.imooc; public class Dog extends Animal { public void eat() { System.out.print("狗具有吃肉的能力"); } public void watchDoor() { System.out.print("狗具有看門的能力"); } }
/javaDemo4/src/com/imooc/Initail.java
package com.imooc; public class Initail { public static void main(String[] args) { // TODO Auto-generated method stub Animal obj1 = new Animal(); Animal obj2 = new Dog(); //父類的引用是可以指向子類對象 //Dog obj3 = new Animal(); //報錯 Animal obj3 = new Cat(); obj1.eat(); obj2.eat(); obj3.eat(); // obj2.watchDoor(); } }
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://systransis.cn/yun/73146.html
摘要:多態(tài)中的引用類型轉換代碼自動類型提升,向上類型轉換向下類型轉換強制類型轉型無法進行類型轉換 多態(tài)中的引用類型轉換 showImg(https://segmentfault.com/img/bVbnEal?w=1166&h=597); showImg(https://segmentfault.com/img/bVbnEaq?w=1178&h=606); 代碼 /javaDemo4/src...
摘要:中的接口實例智能手機和都有玩游戲的功能代碼具有玩游玩的功能具有玩游玩的功能 java中的接口 showImg(https://segmentfault.com/img/bVbnEzV?w=1194&h=621); showImg(https://segmentfault.com/img/bVbnEzX?w=1192&h=615); showImg(https://segmentfaul...
摘要:中的抽象類代碼實現(xiàn)通過鍵盤來打電話通過鍵盤來打短信通過語音來打電話通過語音來發(fā)短信運行 java中的抽象類 showImg(https://segmentfault.com/img/bVbnEiG?w=1189&h=613); showImg(https://segmentfault.com/img/bVbnEiH?w=1214&h=620); showImg(https://segm...
摘要:中的繼承初始化順序父類和子類年齡動物可以吃東西類執(zhí)行了年齡狗可以吃東西類執(zhí)行了對象的屬性和構造方法年齡動物可以吃東西類執(zhí)行了的 java中的繼承初始化順序 showImg(https://segmentfault.com/img/bVbnBI1?w=1277&h=671); showImg(https://segmentfault.com/img/bVbnBKG?w=811&h=427...
java中的方法重寫 showImg(https://segmentfault.com/img/bVbnBEj?w=1260&h=668); 重寫前 showImg(https://segmentfault.com/img/bVbnBEu?w=347&h=158); /javaDemo3/src/com/imooc/Animal.java package com.imooc; public cl...
閱讀 1280·2021-09-01 10:30
閱讀 2174·2021-07-23 10:38
閱讀 927·2019-08-29 15:06
閱讀 3181·2019-08-29 13:53
閱讀 3300·2019-08-26 11:54
閱讀 1862·2019-08-26 11:38
閱讀 2401·2019-08-26 10:29
閱讀 3155·2019-08-23 18:15