Problem Description
1.实验目的
(1) 熟悉类的创建方法
(2) 掌握对象的声明与创建
(3) 能利用面向对象的思想解决一般问题
2.实验内容
编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类,包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
3.实验要求
补充完整下面的代码
public class Main{
public static void main(String[] args){
Vehicle v=new Vehicle(8,10.00);
smallCar c=new smallCar(6);
Truck t=new Truck(10);
v.disMessage();
c.disM();
t.disM2();
t.disM3();
}
}
// 你的代码
Sample Output
The number of wheels in this car is 8, weight is 10.0
This car can carry 6 persons
The load of this truck is 10
The number of wheels in this truck is 8, weight is 10.0, can carry 6 persons, load of this truck is 10
我的代码:
public class Main{
public static void main(String[] args){
Vehicle v=new Vehicle(8,10.00);
smallCar c = new smallCar(6);
Truck t=new Truck(10);
v.disMessage();
c.disM();
t.disM2();
t.disM3();
}
}
// 你的代码
class Vehicle{
//车轮的个数
static int wheels;
//车重
static double weight;
public Vehicle(){}
public Vehicle(int i, double v) {
wheels = i;
weight = v;
}
public void disMessage() {
System.out.println("The number of wheels in this car is "+ wheels + ", weight is " + weight);
}
}
class smallCar extends Vehicle{
//载人数
static int loader;
public smallCar() {
}
public void disM(){
System.out.println("This car can carry "+ loader +" persons");
}
public smallCar(int i){
super();
loader = i;
}
public int getWheels(){
return wheels;
}
public double getWeight(){
return weight;
}
}
class Truck extends smallCar{
//载重量
static int payload;
public Truck(int i){
super();
payload = i;
}
public void disM2(){
System.out.println("The load of this truck is " + payload);
}
public void disM3(){
System.out.println("The number of wheels in this truck is " + wheels + ", weight is " + weight+
", can " +
"carry " + loader + " persons," +
" " +
"load of this truck is " + payload);
}
}