博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象的应用
阅读量:4972 次
发布时间:2019-06-12

本文共 2669 字,大约阅读时间需要 8 分钟。

被客户不断变化的需求“折磨”:

开发一个打印机控制程序,要能够实现开机,关机和打印功能

class Printer{

         void open(){

                   System.out.println("Open");

         }

        

         void close(){

                   System.out.println("Close");

         }

        

         void print(String s){

                   System.out.println("print-->"+s);

         }

}

class Test{

         public static void main(String args[]){

                   Printer printer = new Printer();

                   Printer.open();

                   Printer.print("abc");

                   Printer.close();

         }

}

办公室新加了一台打印机,控制两台打印机(不是同一个牌子)

class HPPrinter{

         void open(){

                   System.out.println("Open");

         }

        

         void close(){

                   System.out.println("Close");

         }

        

         void print(String s){

                   System.out.println("print-->"+s);

         }

}

class CanonPrinter extends Printer{

         void open(){

                   System.out.println("Open");

         }

        

         void close(){

                   this.clean();

                   System.out.println("Close");

         }

        

         void print(String s){

                   System.out.println("print-->"+s);

         }

         void clean(){

                   System.out.println("clean”);

         }

class Test{

         public static void main(String args[]){

                   int flag = 0;//flag用于模拟用户的输入0或1;

                  

                   if(flag == 0){

                            HPPrinter hpPrinter = new HPPrinter();

                            hpPRinter.open();

                            hpPRinter.print("abc");

                            hpPRinter.close();

                   }

                   else if(flag == 1){

                            CanonPrinter canonPrinter = new CannonPrinter();

                            CanonPrinter.open();

                            CanonPrinter.print("abc");

                            CanonPrinter.close();

                   }

         }

}

大量的重复代码,修改如下:

class HPPrinter extends Printer{

}

class CanonPrinter extends Printer{

         void close(){

                   this.clean();

                   super.close();//调用父类中的close代码

         }

         void clean(){

                   System.out.println("Close");

         }

}

class Printer{

         void open(){

                   System.out.println("Open");

         }

        

         void close(){

                   System.out.println("Close");

         }

        

         void print(String s){

                   System.out.println("print-->"+s);

         }

}

}

class Test{

         public static void main(String args[]){

                   int flag = 0;//flag用于模拟用户的输入0或1;

                  

                   if(flag == 0){

                            HPPrinter hpPrinter = new HPPrinter();

                            hpPRinter.open();

                            hpPRinter.print("abc");

                            hpPRinter.close();

                   }

                   else if(flag == 1){

                            CanonPrinter canonPrinter = new CannonPrinter();

                            CanonPrinter.open();

                            CanonPrinter.print("abc");

                            CanonPrinter.close();

                   }

         }

}

办公室新加了一台打印机,控制三台打印机(不是同一个牌子)

class HPPrinter extends Printer{

}

class CanonPrinter extends Printer{

         void close(){

                   this.clean();

                   super.close();//调用父类中的close代码

         }

         void clean(){

                   System.out.println("Close");

         }

}

class Printer{

         void open(){

                   System.out.println("Open");

         }

        

         void close(){

                   System.out.println("Close");

         }

        

         void print(String s){

                   System.out.println("print-->"+s);

         }

}

}

class ShenzhouPrinter extends Printer{

}

 

class Test{

         public static void main(String args[]){

                   int flag = 0;//flag用于模拟用户的输入0或1;

                  

                   if(flag == 0){

                            HPPrinter hpPrinter = new HPPrinter();

                            hpPRinter.open();

                            hpPRinter.print("abc");

                            hpPRinter.close();

                   }

                   else if(flag == 1){

                            CanonPrinter canonPrinter = new CannonPrinter();

                            CanonPrinter.open();

                            CanonPrinter.print("abc");

                            CanonPrinter.close();

                   }

         }

}

 

 

转载于:https://www.cnblogs.com/java4android/archive/2012/04/27/2473316.html

你可能感兴趣的文章
IDEA使用操作文档
查看>>
如何对网课、游戏直播等进行录屏
查看>>
UIView
查看>>
有关去掉谷歌及火狐浏览器文本框 数字类型 上下箭头的方法
查看>>
MySQL数据迁移到SQL Server
查看>>
复杂链表的复制(python)
查看>>
添加日期选择控件
查看>>
jquery.cookie.js操作cookie
查看>>
javascript遍历数组
查看>>
bzoj4765: 普通计算姬 (分块 && BIT)
查看>>
thinkphp5-----模板中函数的使用
查看>>
POJ-3211 Washing Clothes[01背包问题]
查看>>
[BZOJ4832][Lydsy1704月赛]抵制克苏恩
查看>>
数据库三范式
查看>>
看完漫画秒懂区块链
查看>>
开发工具,做一个有效率的开发者
查看>>
对Haskell这门语言的基本认识
查看>>
mysql 安装补充
查看>>
大学里如何学习 ?
查看>>
Oracle命令类别
查看>>