182页习题16

来源:互联网 发布:淘宝网禁止出售滥用 编辑:程序博客网 时间:2024/04/28 23:10
/** * Exercise 16: (2) Create a class called Amphibian.  * From this, inherit a class called Frog. Put appropriate methods in the base class.  * In main( ), create a Frog and upcast it to Amphibian  * and demonstrate that all the methods still work. */package reusing;class Amphibian{Amphibian(){}void doHibernate(){System.out.println("Amphibian都冬眠");}//因为有了构造函数, 所以下面可以用Amphibian类型的变量了static void wakeUp(Amphibian x){x.doHibernate();System.out.println("春天来了, 该醒醒了!");}}public class Frog extends Amphibian{public static void main(String[] args) {Frog y=new Frog();//注意下面有个upcast, y是frog, 但在wakeUp中,//被自动upcast成了amphibian, //因为frog is an amphibian, 所以可以在wakeUp()中使用Amphibian.wakeUp(y);}}//output://Amphibian都冬眠//春天来了, 该醒醒了!