首页 > 代码库 > 作业-继承7
作业-继承7
编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数
wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数
loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个
类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功
能。
package T092206;public class Vehicle { private int wheels; private double weight; public int getWheels() { return wheels; } public void setWheels(int wheels) { this.wheels = wheels; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } Vehicle(){ } Vehicle(int wheels,double weight){ this.wheels=wheels; this.weight=weight; } public void getInfo(){ System.out.println("车轮的个数是:"+wheels+" 车重:"+weight); }}package T092206;public class Car extends Vehicle { private int loader; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } Car(){ } Car(int wheels,double weight,int loader){ super(wheels,weight); this.loader=loader; } public void loader(){ if(loader<=6){ System.out.println("这辆车能载"+loader+"人"); } else{ System.out.println("超员了"); } }}package T092206;public class Truck extends Car { Truck(){ super(); } private double payload; public double getPayload() { return payload; } public void setPayload(double payload) { this.payload = payload; } public void payload(){ if(payload<=1000){ System.out.println("这辆车的载重是"+payload); } else{ System.out.println("超重了"); } }}package T092206;public class Test { public static void main(String[] args){ Car c=new Car(4,1000,4); c.getInfo(); c.loader(); Truck t =new Truck(); t.setLoader(3); t.setPayload(2000); t.setWeight(3000); t.setWheels(6); t.getInfo(); t.loader(); t.payload(); }}
作业-继承7
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。