多态(事物存在的多种体现形态)

什么是接口
a.可以认为是一个特殊的抽象类
b.当抽象类中的方法都是抽象的那么该类可以通过接口的形式实现
c.一个类可以实现多个接口
d.当子类对接口中的方法全部实现覆盖后子类才可以实例化,否则子类是一个抽象类
e.不可以实现多继承是因为父类的方法有重复,而接口中的方法都是抽象方法
f.接口亦可以实现多继承
接口的特点
基本功能在类中,扩展功能在接口中。

1.多态的表现

父类的引用可以接受自己子类的对象

Animal a= new Cat(); //类型提升
Cat c = (Cat) a;     //转成子类型

2.多态的前提

必须是类和类之间的关系(存在覆盖)
a.继承
b.实现

3.多态到底有什么好处(大大提高了程序的扩展性 )

4.多态的应用

/*应用一  电脑运行实例
电脑运行依靠主板
*/
interface PCI
{
    public void open();
    public void close();
}

class NetCard implements PCI
{
    public void open()
    {
        System.out.println("NetCard open");
    }
    public void close()
    {
        System.out.println("NetCard close");
    }
}

class SoundCard implements PCI
{
    public void open()
    {
        System.out.println("SoundCard open");
    }
    public void close()
    {
        System.out.println("SoundCard close");
    }
}

class MainBoard
{
    public void run()
    {
        System.out.println("MainBoard run");
    }
    public void usePCI(PCI p) //PcI p =new NetCard();接口型对象指向自己的子类对象
    {
        p.open();
        p.close();
    }
}

public class  DuoTaiDemo5
{
    public static void main(String[] args)
   {
      MainBoard mb = new MainBoard();
      mb.run();
      mb.usePCI(new NetCard()); //将NetCard对象当作PCI类型来用
      mb.usePCI(new SoundCard()); //SoundCard对象当作PCI类型来用
}
/*应用二  模拟物流快递系程序设计
1.任务描述 网购成为人们生活的一部分,当人们在购物网站中下订单后,订单中的货物就会在一系列的流程后送到客户手中。
2.实现思路
1)定义一个抽象的交通工具类,类中包含交通工具的编号,型号以及运货负责人等属性,还要定义一个抽象的运输方法。
2)运输完成后需要对工具进行保养。所以定义保养接口
3)定义一个专用运输车类,继承交通工具类
4)定义一个快递任务类,包含快递单号,货物重量以及发货前和发货后的方法。
5)定义一个GPS接口,以及实现该接口的仪器类(如iPhone等)
*/

  abstract class Transportation
  {
      private String number; //编号
      private String model;  //型号
      private String admin;  //送货员
      public Transportation (){
      super(); //可省略
      }
      //构造方法
      public Transportation(String number, String model, String admin){
             this.number=number;   //在构造方法中使用this.number表示访问成员变量,
             this.model=model;     //直接使用number表示访问局部变量
             this.admin=admin;
          }
       //运输方法。
       public abstract void transport();
       //编号
       public void setNumber(String number){
       this.number=number;
       }
       public String getNumber(){
        return number;
       }
       //型号
       public void setModel(){
        this.model=model;
       }
       public String getModel(){
        return model;
       }
       //送货员
       public void setAdmin(){
        this.admin=admin;
       }
       public String getAdmin(){
        return admin;
       }
  }

  //定义交通工具保养接口Careable
  interface Careable
  {
      public abstract void upKeep();
  }

  //定义专用运输车类
  class ZTransportation extends Transportation implements Careable 
  {
      //无参构造
      public ZTransportation (){
      super();
      }
      //有参构造:车辆编号,型号,负责人
      public ZTransportation(String number, String model, String admin){
      super(number,model,admin);
      }
      //运输方法
      public void transport(){
       System.out.println("货物正在运输中");
      }
      public void upKeep(){
      System.out.println("货物运送车辆保养完毕");
      }
  }

  //定义快递任务类SendTask
  class SendTask
  {
     private String number;  //快递单号
     private double goodsWeight;
     public SendTask(){
     super();
     }
     public SendTask(String number, double goodsWeight){
     this.number=number;
     this.goodsWeight=goodsWeight;
     }
     //送前准备
     public void SendBefore(){
     System.out.println("订单开始处理,仓库验货中");
     System.out.println("货物重量:"+ this.getGoodsWeight()+"kg");
     System.out.println("货物检验完毕");
     System.out.println("货物装填完毕");
     System.out.println("运货人已通知");
     System.out.println("快递单号:"+this.getNumber());
     }
     //发送货物
     public void send( Transportation t, GPS tool){
     System.out.println("运货人:"+t.getAdmin()+"正在驾驶编号是:"+
     t.getNumber()+"的"+t.getModel()+"发送货物");
     t.transport();
     String showCoordinate=tool.showCoordinate();
     System.out.println("货物的当前坐标为:"+showCoordinate);
     }
     //送后操作
     public void sendAfter(Transportation t){
      System.out.println("货物运输任务已完成");
      System.out.println("运货人:"+t.getAdmin()+"所驾驶的编号是:"+
      t.getNumber()+"的"+t.getModel()+"已签收");
     }
     public String getNumber(){
     return number;
     }
     public void setNumber(){
     this.number=number;
     }
     public double getGoodsWeight(){
     return goodsWeight;
     }
     public void setGoodsWeight(){
     this.goodsWeight=goodsWeight;
     }
  }

  //定义接口GPS并且用类iPhone类实现

  interface GPS
  {
      //the way to show the coordinate
      public String showCoordinate();
  }

  //实现类iPhone
  class iPhone implements GPS
  {
    public iPhone(){
    super();//空构造参数
    }
    public String showCoordinate(){
    String location = "193,24";
    return location;
    }
  }

  //定义测试类
  public class TransportationTest
  {
      public static void main(String[] args)
      {
        //快递任务类对象
        SendTask task=new SendTask("HYX600235",76.34);
        //发送前状态
        task.SendBefore();
        System.out.println("----------------------");
        System.out.println("----------------------");
        //创建交通工具类对象
        ZTransportation t =new ZTransportation("Z205","Benz","小罗");
        //创建GPS对象
        iPhone i=new iPhone();
        //发送货物时的状态
        task.send(t,i);
        //发送后方法调用
        System.out.println("----------------------");
        System.out.println("----------------------");
        task.sendAfter(t);
        t.upKeep();
      }
  }


5.多态的弊端

提高了扩展性,但只能引用父类的引用访问父类中的成员。

if (a instanceof Cat) {}
if (a instanceof Dog) {}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值