Java中方法的重写与继承例题
- 题目1
1.设计一个汽车类Vehicle,包含的数据成员有车轮个数wheels和车重weight。
2.小车类Car是它的公有派生类其中包含载人passenger。
3.卡车类Truck是Vehicle的公有派生类其中包含载人数passenger和载重量pay。
4.每个类都有相关数据的输出函数,用以测试构造函数调用顺序,以及成员变量初始化及数据输出等。
输入:
8 5
4 1 5
6 8 2 10
输出:
car:
Manned number:8 Bit
Capacity:5.0 Tons
a car:
Manned number:4 Bit
Capacity:1.0 Tons
Manned number:5 Bit
truck:
Manned number:6 Bit
Capacity:8.0 Tons
Manned number:2 Bit
Capacity:10.0 Tons
//package LA;
import java.util.Scanner;
class Vehicle{
private int wh;
private int we;
public Vehicle(int wh,int we){
super();
this.wh = wh;
this.we = we;
}
public void show() {
System.out.println("car:");
}
public void print() {
System.out.println("Manned number:" + wh + " Bit");
System.out.println("Capacity:" + we + ".0" + " Tons");
}
public int getWh() {
return wh;
}
public void setWh(int wh) {
this.wh = wh;
}
public int getWe() {
return we;
}
public void setWe(int we) {
this.we = we;
}
}
class Car extends Vehicle{
private int pa;
public void print() {
System.out.println("a car:");
super.print();
System.out.println("Manned number:" + pa + " Bit");
}
public int getPa() {
return pa;
}
public void setPa(int pa) {
this.pa = pa;
}
public Car(int wh, int we, int pa){