看有人说要给功能作为接口,让两个子类实现

来源:12-1 综合练习

攻城狮1994

2016-08-29 18:14

这样的意义何在?格式化输出容量么?

写回答 关注

4回答

  • 慕移动9181930
    2022-03-26 05:40:38

    这个问题前面有人问过,也有人回答了。是因为right的高度比left大造成的,而且应该是在ie8+的浏览器才有的问题。

  • 攻城狮1994
    2016-08-30 10:50:46
    package muke.stu.one.serivce;
     //运输货物的接口
    public interface transportGoods {
        public String transportGoods();
    }
    package muke.stu.one.serivce;
      //运输人的接口
    public interface transportPeople {
        public  String transportPeople();
    }
    //实体类:车辆
    public class Car {
        //名字,类型
        private String type;
        //价钱
        private double price;
        //容量
        private String content;
        //对应的get,set
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
         
    }
     //pika子类
    package muke.stu.one.entity;
     
    import muke.stu.one.serivce.transportGoods;
    import muke.stu.one.serivce.transportPeople;
     
    public class Pika extends Car implements transportGoods,transportPeople{
        //载货量
        private int capacity;
        //载人量
        private int manned;
         
        public int getCapacity() {
            return capacity;
        }
        public void setCapacity(int capacity) {
            this.capacity = capacity;
        }
        public int getManned() {
            return manned;
        }
        public void setManned(int manned) {
            this.manned = manned;
        }
        //重写构造方法
        public Pika(){
            super();
        }
        public Pika(String type,int manned,int capacity,double price){
            this.setType(type);
            this.setCapacity(capacity);
            this.setManned(manned);
            this.setPrice(price);
            //容量就是连个接口的内容
            this.setContent(transportPeople()+transportGoods());
        }
         
         //实现接口
        @Override
        public String transportPeople() {
            String transportPeople ="载人量:"+this.getManned();
            return transportPeople;
             
        }
     
        @Override
        public String transportGoods() {
            String transportGoods="载货量:"+this.getCapacity();
            return transportGoods;
        }
         
    }
     //客车类
    package muke.stu.one.entity;
     
    import muke.stu.one.serivce.transportPeople;
     
    public class Coatch extends Car implements transportPeople{
         //载人量
        private int manned;
     
        public int getManned() {
            return manned;
        }
        public void setManned(int manned) {
            this.manned = manned;
        }
        //构造方法
        public Coatch(){
            super();
        }
        public Coatch(String type,int manned,double price){
            this.setType(type);
            this.setManned(manned);
            this.setPrice(price);
            this.setContent(transportPeople());
        }
        //实现接口
        @Override
        public String transportPeople() {
            String transportPeople ="载人量:"+this.getManned();
            return transportPeople;
             
        }
    }
    //货车类
    package muke.stu.one.entity;
     
    import muke.stu.one.serivce.transportGoods;
     
    public class Truck extends Car implements transportGoods{
        //载货量
        private int capacity;
        public int getCapacity() {
            return capacity;
        }
        public void setCapacity(int capacity) {
            this.capacity = capacity;
        }
        //构造方法
        public Truck(){
            super();
        }
        public Truck(String type,int capacity,double price){
            this.setType(type);
            this.setCapacity(capacity);
            this.setPrice(price);
            this.setContent(transportGoods());
        }
        //实现接口
        @Override
        public String transportGoods() {
            String transportGoods="载货量:"+this.getCapacity();
            return transportGoods;
        }
         
    }


  • JustWannaHugU
    2016-08-29 22:26:44

    接口是类,你实现接口那就是他的子类,

    你自然就有接口的方法了,人不管你实现接口的类名是什么,

    只要确定你实现了这个接口就行了,子类对象可以直接拿父类引用来用的。

    这就是java多态性的具体体现!

    同一种功能,不同的对象(子类or父类)实现的具体细节各不相同

    望采纳

    攻城狮199...

    我想问的是,在此接口有何具体作用?

    2016-08-30 10:46:13

    共 1 条回复 >

  • 慕标7315214
    2016-08-29 19:35:48

    接口可以实现多继承,接口中定义的是抽象方法,接口需要你去实现。

Java入门第二季

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

531537 学习 · 6329 问题

查看课程

相似问题