父类:
class Printer{ void open(){ System.out.print("Open"); } void close(){ System.out.print("close"); } void print(String print){ System.out.print("print-->"+print); } }
子类:
class HPPrinter extends Printer{ }
class CanonPrinter extends Printer{ void close(){ this.clean(); super.close(); } void clean(){ System.out.print("clean"); } }
主函数:
class Test{ public static void main(String args[]){ int flag = 0; if(flag == 0){ HPPrinter hpPrinter = new HPPrinter(); hpPrinter.open(); hpPrinter.print("bvc"); hpPrinter.close(); } else if(flag == 1){ CanonPrinter canonPriter = new CanonPrinter(); canonPriter.open(); canonPriter.print("bvc"); canonPriter.close(); } } }