看有人说要给功能作为接口,让两个子类实现
这样的意义何在?格式化输出容量么?
这样的意义何在?格式化输出容量么?
2016-08-29
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; } }
举报