1.定义一个模板的抽象类House,里面有模板抽象方法
2.定义一个实现模板类LargeBuilding,该类继承House,重写House的模板抽象方法 3.测试,多态1.
public abstract class House {public abstract void function();public void Getfunction(){ System.out.println("模板房子前"); function(); System.out.println("模板房子后");}}
2.
//大厦public class LargeBuilding extends House{ @Override public void function() { System.out.println("大厦办公"); }}//餐厅public class Restaurant extends House { @Override public void function() { System.out.println("餐厅吃饭"); }}
3.
public class Test {public static void main(String[] args) { House House1=new LargeBuilding(); House House2=new Restaurant(); House1.Getfunction(); System.out.println("--------------"); House2.Getfunction();}}输出:模板房子前大厦办公模板房子后--------------模板房子前大厦办公模板房子后