java学习记录——类和对象的练习4

(习题)编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类,包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

 
class Vehicle{
	public int wheels;
	public double weight;
	public Vehicle(int wheels,double weight){
		this.wheels = wheels;
		this.weight = weight;
	}
	public void Print1(){
		System.out.println("车轮个数为:" + wheels + " 个");
		System.out.println("车重为    :" + weight + " 吨");
	}
}
class Car extends Vehicle{
	public int loader;
	public Car(int wheels,double weight,int loader){
		super(wheels,weight);
		this.loader = loader;
	}
	public void Print2(){
		System.out.println("限载人数为:" + loader + " 人");
	} 
}
class Truck extends Car{
	public double payload;
	public Truck(int wheels,double weight,int loader,double payload){
		super(wheels,weight,loader);
		this.payload = payload;
	}
	public void Print3(){
		System.out.println("限载重量为:" + payload + " 吨");
	}
}
public class Test4_3{
	public static void main(String[] args){
		Truck t = new Truck(4,78.3,5,23.7);
		t.Print1();
		t.Print2();
		t.Print3();
	} 
}

本人初学,如有疑问或不对之处请指出。欢迎交流。

猜你喜欢

转载自blog.csdn.net/dreamer0823/article/details/78456565