定义一个交通类Transport,声明两个私有属性speed和load分别表示速度和载重量,声明读取属性和修改属性的方法,声明显示方法show 0用来打印读取的数据,在构造方法里初始化属性值。心(1)定义汽车类Vehicles继承交通类,增加两个属性wheels和weight分别表示车轮数和车重量,声明读取属性和修改属性的方法,定义构造方法初始化所有属性《调用父类的构造方法》,覆盖父类的显示方法show()打印所有属性值(调用父类的显示方法)。4(2)在main方法里分别创建汽车类对象,通过构造方法传值,再调用show方法。p
public class Transport {
private int speed;
private int load;
public Transport(int speed, int load) {
super();
this.speed = speed;
this.load = load;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public void show() {
System.out.println("speed=" + speed + ", load=" + load);
}
}
public class Vehicles extends Transport{
private int wheels;
private int weight;
public Vehicles(int speed, int load, int wheels, int weight) {
super(speed, load);
this.wheels = wheels;
this.weight = weight;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void show() {
super.show();
System.out.println("wheels=" + wheels + ", weight=" + weight);
}
}
public class Test {
public static void main(String[] args) {
Vehicles v = new Vehicles(1,2,3,4);
v.show();
}
}