设计一个汽车类,属性有车轮个数与车重,卡车类是汽车类的子类,新增加的属性是载重量,面包车类是卡车类的子类,新增加的属性是载客数,要求每个类都有一个输出方法,用于输出相应的信息。
执行代码:
public class qichelei {
protected int wheels;
protected double weight;
public qichelei(int wheels, double weight) {
this.wheels = wheels;
this.weight = weight;
}
public String toString() {
return "qichelei[wheels=" + wheels + ", weight=" + weight + "]";
}
}
class kachelei extends qichelei {
protected int loader;
public kachelei(int wheels, double weight, int loader) {
super(wheels, weight);
this.loader = loader;
}
public String toString() {
return "kachelei[loader=" + loader + ", wheels=" + wheels + ", weight=" + weight +"]";
}
}
class mianbaochelei extends kachelei {
private double payload;
public mianbaochelei(int wheels, double weight, int loader, double payload) {
super(wheels, weight, loader);
this.payload = payload;
}
public String toString() {
return "mianbaochelei[" +
"payload=" + payload +
", loader=" + loader +
", wheels=" + wheels +
", weight=" + weight +
"]";
}
}
class test {
public static void main(String[] args) {
mianbaochelei mianbaochelei = new mianbaochelei(4,400,7,400);
System.out.println(mianbaochelei);
kachelei kachelei = new kachelei(8,500,3);
System.out.println(kachelei);
qichelei qichelei = new qichelei(2,300);
System.out.println(qichelei);
}
}
执行结果:
·先调试后运行