综合题:继承(二次继承)、重写

在这里插入图片描述

package item3;
/**
 * 第三次上机接口练习
 * @author Administrator
 *
 */
class Retangle{
	private int length;
	private int width;
	public void setLength(int l) {
		this.length=l;
	}
	public int getLength() {
		return length;
	}
	public void setWidth(int w) {
		this.width=w;
	}
	public int getWidth() {
		return width;
	}
	public int getArea() {
		return width*length;
	}
}
class Cuboid extends Retangle{
	private int height;
	public void setHeight(int h) {
		this.height=h;
	}
	public int getHeight() {
		return height;
	}
	public int getVolume(){
		return height*super.getArea();
	}	
}

public class Main3 {
	public static void main(String[] args) {
		Cuboid c1=new Cuboid();
		c1.setLength(3);
		c1.setWidth(4);
		c1.setHeight(5);
		c1.getArea();
		System.out.println("Area is"+c1.getArea());
		c1.getVolume();
		System.out.println("Volume is"+c1.getVolume());
		
	}

}

在这里插入图片描述

package item3;
         //vehicle类
class Vehicle{
	private int wheels;
	private double weight;
	//构造方法:初始化weight为1,wheels为4
	public Vehicle() {
		weight=1;
		wheels=4;
	}
	//构造方法:用参数设置weight、wheels
	public Vehicle(int wh,double we) {
		wheels=wh;
		weight=we;
	}
	//返回值
	public void setWheels(int wh) {
		this.wheels=wh;
	}
	public int getWheels() {
		return wheels;
	}
	public void setWeight(double we) {
		this.weight=we;
	}
	public double getWeight() {
		return weight;
	}
}
       //Car类
       //继承Vehicle
class Car extends Vehicle{
	   //定义成员变量loader
	private int loader;
	   //构造方法:初始化loader为4
	public Car() {
		loader=4;
	}   
	   //用参数设置loader
	public Car(int renshu) {
		this.loader=renshu;
	}  
	    //重写父类Vehicle的方法、super()访问父类成员方法wheels()、weight()
	public Car(int wh,double we,int renshu) {
		super(wh,we);
		this.loader=renshu;
	}
	public void setLaoder(int renshu) {
		this.loader=renshu;
	}
	public int getLoader() {
		return loader;
	}
}
          // Truck类
class Truck extends Car{
	private double payload;
	public Truck() {
		payload=10;
	}
	public Truck(double pl) {
		this.payload=pl;
	}
	//调用父类及间接父类成员方法
	public Truck(int wh,double we,int renshu,double pl) {
		super(wh,we,renshu);
		this.payload=pl;
	}
	public void setPayload(double pl) {
		this.payload=pl;
	}
	public double getPayload() {
		return payload;
	}
}
public class Main2 {
	public static void main(String[] args) {
		Vehicle c1=new Vehicle();
		System.out.print("汽车c1有"+c1.getWheels()+"个轮子");
		System.out.println("它的重量是"+c1.getWeight()+"顿");
		Car c2=new Car();
		System.out.print("汽车c2有"+c2.getWheels()+"个轮子");
		System.out.print("它的重量是"+c2.getWeight()+"顿");
		System.out.println("能载重"+c2.getLoader()+"个人");
		Truck c3=new Truck(8,3,4,100);
		System.out.print("汽车c2有"+c3.getWheels()+"个轮子");
		System.out.print("它的重量是"+c3.getWeight()+"顿");
		System.out.print("能载重"+c3.getLoader()+"个人");
		System.out .println("能装"+c3.getPayload()+"吨货");
		
	}
}

汽车c1有4个轮子它的重量是1.0顿
汽车c2有4个轮子它的重量是1.0顿能载重4个人
汽车c2有8个轮子它的重量是3.0顿能载重4个人能装100.0吨货

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值