向上转型编译出问题,继承应该没问题
package polymorphism;import polymorphism.cycle.*;
public class E01_Upcasting {
public static void ride(Cycle c) {}
public static void main(String[] args) {
ride(new Cycle()); // No upcasting
ride(new Unicycle()); // Upcast
ride(new Bicycle()); // Upcast
ride(new Tricycle()); // Upcast
}
}
//: polymorphism/cycle/Cycle.java
package polymorphism.cycle;
public class Cycle {
} ///:~
//: polymorphism/cycle/Unicycle.java
package polymorphism.cycle;
public class Unicycle extends Cycle {
} ///:~
//: polymorphism/cycle/Bicycle.java
package polymorphism.cycle;
public class Bicycle extends Cycle {
} ///:~
//: polymorphism/cycle/Tricycle.java
package polymorphism.cycle;
public class Tricycle extends Cycle {
}