多态的应用

public abstract class Report {

         public abstract void getReport();

}

public class AgentReport extends Report{

 

         public void getReport() {

                   System.out.println("代理的佣金计划");

         }

 

}

public class ClientReport extends Report{

 

         public void getReport() {

                   System.out.println("客户的佣金计划");

         }

 

}

/**

 * 这样编程会被打死的,因为如果要查看客户的报表,你还要重新编程Admin

 * 代码耦合性太强,复用性太低了

 * @author Administrator

 *

 */

public class Admin_0 {

         private AgentReportagentReport;

         public void setAgentReport(AgentReport agentReport) {

                   this.agentReport =agentReport;

         }

         public void checkReport(){

                   System.out.println("管理员查看报表");

                   agentReport.getReport();

         }

        

         public static void main(String[] args) {

                   Admin_0 admin =new Admin_0();

                   AgentReport agentReport =new AgentReport();

                   admin.setAgentReport(agentReport);

                   admin.checkReport();

         }

        

}

/**

 * 应用多态,可以解决上述为题

 * @author Administrator

 *

 */

public class Admin_1 {

         private Reportreport;

         public void setReport(Report report) {

                   this.report =report;

         }

 

         public void checkReport(){

                   System.out.println("管理员查看报表");

                   report.getReport();

         }

        

         public static void main(String[] args) {

                   Admin_1 admin_1 =new Admin_1();

                   Report report =new AgentReport();//这就是多态的正确应用

                   admin_1.setReport(report);

                   admin_1.checkReport();

                  

                   Report report2 =new ClientReport();

                   admin_1.setReport(report2);

                   admin_1.checkReport();

         }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值