通过几何父类来比较子类的面积大小

这篇博客探讨了如何通过创建一个几何抽象类Geometrict,来实现比较Circle和Rectangle子类的面积大小。作者作为初学者,详细介绍了实现过程,并在代码中加入了个人分析,期待读者指正。
摘要由CSDN通过智能技术生成

几何类Geometrict.java

package geometrict;

public abstract class Geometrict  {
//	对数据进行封装保护
	private String color;
	private boolean isFilled;
	
//	声明构造方法初始化对象
	public Geometrict() {
		color = "cyan";
		isFilled = false;
	}
//	颜色的访问器及修改器
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
//	是否填充
	public boolean isFilled() {
		return isFilled;
	}
	public void setFilled(boolean isFilled) {
		this.isFilled = isFilled;
	}
	
//	设置获取面积为抽象方法,子类必须实现(重写)该方法
	public abstract double getArea();
	
//	通过父类调用子类来比较面积的大小返回整数值
	public static <E extends Geometrict> int compareArea(E g1, E g2) { //使用泛型来对传入的参数进行限制,
//		只能传入Geometrict类型或Geometrict的子类对象
		if ( g1.getArea()>g2.getArea())
			return 1;
		else if (g1.getArea() < g2.getArea())
			return -1;
		else return 0;
	}
	
//	测试下toString方法
	public String toString(){
		String fill;
		if(isFilled)
			fill = "";
		else
			fill = "不";
		return "图形的初始化颜色为:" + color + " " + fill + "填充";
//		return "1123";
	}
}

Circle类

package geometrict;

public class Circle extends Geometrict {
//	对数据进行封装保护
	public static double PI = 3.14;
	private double radius;
	private String color;
	private boolean isFilled;
	
//	无参以及有参构造方法
	public Circle() {
		radius = 3.0;
		color = "black";
		
	}
	public Circle(double radius) {
		this.radius = radius;
		color = "black";
	}
//	半径的访问器及修改器
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
//	颜色的访问器及修改器(可以不写,已经继承了父类,这是在重写父类方法)
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
//	是否填充
	public boolean isFilled() {
		return isFilled;
	}
	public void setFilled(boolean isFilled) {
		this.isFilled = isFilled;
	}
//	这个方法必须重写,否则这个类就只能设置为抽象类
	@Override
	public double getArea() {
		return PI * radius * radius;
	}
	
	@Override
	public String toString(){
		String fill;
		if(isFilled)
			fill = "";
		else
			fill = "不";
		return "圆的初始化颜色为:" + color + " " + fill + "填充";
//		return "1123";
	}
}

Rectangle类

package geometrict;

public class Rectangle extends Geometrict {
//	对数据进行封装保护
	private double width;
	private double height;
	private String color;
	private boolean isFilled;
	//和圆不一样,没写颜色和填充的修改器和访问器,因为已经继承了父类的方法,如果有需要可以重写,圆的也可以不写,只是做个比较
	
	
//	无参以及有参构造方法
	public Rectangle() {
		width = 1.0;
		height = 1.0;
		color = "red";
		isFilled = false;
	}
	public Rectangle(double width,double height) {
		this.width = width;
		this.height = height;
		color = "red";
		isFilled = false;
	}
	
//	设置宽的访问器及修改器
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
//	设置高的访问器及修改器
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	
	@Override
	public double getArea() {
		return width * height;
	}

	@Override
	public String toString(){
		String fill;
		if(isFilled)
			fill = "";
		else
			fill = "不";
		return "矩形的初始化颜色为:" + color + " " + fill + "填充";
//		return "1123";
	}
}

Main方法

package geometrict;

public class Main {

	public static void main(String[] args) {
		
		
		Circle circle = new Circle(1);
		System.out.println("圆的半径为:"+circle.getRadius());
		System.out.println("圆的面积为:"+circle.getArea());

		Rectangle r = new Rectangle(1,3.14);
		System.out.println("矩形的宽为:" + r.getWidth() + ",矩形的高为:" + r.getHeight() );
		System.out.println("矩形的面积为:"+r.getArea());
		
		int t = Geometrict.compareArea(circle, r);
		
		if(t == 1)
			System.out.println("圆的面积比矩形的面积大");
		if(t == -1)
			System.out.println("圆的面积比矩形的面积小");
		if(t == 0)
			System.out.println("圆的面积等于矩形的面积");
		System.out.println(r.toString());
		System.out.println(circle.toString());
	}

}

初学者,不懂页面规划,都写在了程序里面,里面也有一些自己的分析,有错误的地方希望指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值