Java【基础学习】向下转型和上转型例子
Java小白应付期末考试QWQ
1 class Animal{ 2 public void move() { 3 System.out.println(1); 4 } 5 } 6 class Dog extends Animal{ 7 public void move() { 8 System.out.println(2); 9 } 10 public void bark() { 11 System.out.println(3); 12 } 13 } 14 public class Main{ 15 public static void main(String[] args) { 16 Animal a1=new Animal(); 17 a1.move(); 18 Animal a2=new Dog(); 19 a2.move(); 20 Dog a3=(Dog)a2; 21 a3.bark(); 22 } 23 }