- class Dolphin extends Pet {
- public void incrediBubble() {
- System.out.println("神奇泡泡打砖块");}
- }
- public class Test {
- public static void showPlay(Pet pet) {//问题所在:子类Dolphin无法代换父类
- if (pet instanceof Dog) {
- Dog dog = (Dog) pet;
- dog.catchingFlyDisc();
- } else if (pet instanceof Penguin) {
- Penguin pgn = (Penguin) pet;
- pgn.swimming();
- }
- }
- public static void main(String[] args) {
- Dolphin dolphin = new Dolphin();
- showPlay(dolphin);
- }
- }
复制代码 |
|