求助,关于继承类的设计与实现java

weixin_45038258 2019-10-03 01:05:20
继承类的设计与实现,一个矩形类,正方形类,长方形类,还有一个测试类
...全文
64 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yeva  2019-10-03
  • 打赏
  • 举报
回复
/**
 * 矩形类,包含长宽,计算面积方法*/
public class Rectangle {
	double length,width;
	
	public double computeArea() {
		return length*width;
	}
	
	
	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}
	
}
/**
 * 正方形类,面积方法重写*/
public class Square extends Rectangle{
	@Override
	public double computeArea() {
		return length*length;
	}
}
/**
 * 长方体类, 有长宽高,计算体积方法*/
public class Cuboid extends Rectangle{
	double hight;

	public double getHight() {
		return hight;
	}

	public void setHight(double hight) {
		this.hight = hight;
	}

	public double computeVolume() {
		return length*width*hight;
	}
}
/**
 * 测试类*/
public class Test {
	public static void main(String[] args) {
		Cuboid cuboid = new Cuboid();
		cuboid.setLength(3.5);
		cuboid.setWidth(2.7);
		cuboid.setHight(2);
		System.out.println("长方体底面积:"+cuboid.computeArea());
		System.out.println("长方体体积:"+cuboid.computeVolume());
		
		Square square = new Square();
		square.setLength(3.5);
		System.out.println("正方形面积:"+square.computeArea());
	}
}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧