抽象类:
package com.secbro.test.abstractInit;
/**
* @author zhuzhisheng
* @Description
* @date on 2016/5/28.
*/
public abstract class Fruit {
protected Fruit(){
System.out.println("实例化父类");
toPlant();
toEat();
}
protected abstract void toEat();
protected abstract void toPlant();
}
子类Banana:
package com.secbro.test.abstractInit;
/**
* @author zhuzhisheng
* @Description
* @date on 2016/5/28.
*/
public class Banana extends Fruit{
protected Banana(){
System.out.println("实例化子类 Banana");
}
@Override
protected void toEat() {
System.out.println("Eat a Banana");
}
@Override
protected void toPlant() {
System.out.println("Plant a Banana");
}
}
子类Orange:
package com.secbro.test.abstractInit;
/**
* @author zhuzhisheng
* @Description
* @date on 2016/5/28.
*/
public class Orange extends Fruit{
public Orange() {
System.out.println("实例化子类 Orange");
}
@Override
protected void toEat() {
System.out.println("Eat a Orange");
}
@Override
protected void toPlant() {
System.out.println("Plant a Orange");
}
}
测试类:
package com.secbro.test.abstractInit;
/**
* @author zhuzhisheng
* @Description
* @date on 2016/5/28.
*/
public class TestFruit {
public static void main(String[] args) {
new Banana();
new Orange();
}
}
执行结果打印:
实例化父类
Plant a Banana
Eat a Banana
实例化子类 Banana
实例化父类
Plant a Orange
Eat a Orange
实例化子类 Orange