java基础:面向对象编程(中)4-多态性课后练习

package com.atguigu.exer;

public class GeometricObject {
	protected String color;
	protected double weight;
	
	protected GeometricObject(String color, double weight) {
		this.color = color;
		this.weight = weight;
	}
	
	/*
	 * 求图像面积
	 * 思考:该方法有定义的必要吗? 有,为 了多态。
	 */
	public double findArea(){
		return 0;
	}
	
	protected String getColor() {
		return color;
	}
	protected void setColor(String color) {
		this.color = color;
	}
	protected double getWeight() {
		return weight;
	}
	protected void setWeight(double weight) {
		this.weight = weight;
	}

	
	
}
 

 

package com.atguigu.exer;

public class Circle extends GeometricObject {

	private double radius;
	public Circle(double radius,String color, double weight) {
		super(color, weight);
		this.radius = radius;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public double findArea() {
		return Math.PI*radius*radius;
	}


	public double getRadius() {
		return radius;
	}


	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	
}
package com.atguigu.exer;

public class Myrectangle extends GeometricObject {
	private double width;
	private double height;
	
	public Myrectangle(double width,double height,String color, double weight) {
		super(color, weight);
		this.height = height;
		this.width = width;		
	}

	@Override
	public double findArea() {
		// TODO Auto-generated method stub
		return width * height;
	}
	
	
}
package com.atguigu.exer;

public class GeometricTest {

/*
 * 定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。
 * 定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
 * 编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。

 */
	
	public static void main(String[] args) {
		GeometricTest gt = new GeometricTest();
	    
		Circle c = new Circle(1.0, "red", 1.0);
		Circle c2 = new Circle(1.0, "red", 1.0);
		Myrectangle mr = new Myrectangle(1.0, 2,"blue", 1.5);
		Myrectangle mr2 = new Myrectangle(1.0, 2,"blue", 1.5);
		
		boolean equalsArea = gt.equalsArea(c, c2);
		System.out.println(equalsArea);
		
	    gt.displayGeometricObject(mr);
	}
	
	/**
	 * 显示面积
	 * @param go
	 */
	private void displayGeometricObject(GeometricObject go) {
				System.out.println(	go.findArea());
				}
	
	/**
	 * 比较面积是否相等
	 * @param go
	 * @param go2
	 * @return
	 */
	public boolean equalsArea(GeometricObject go,GeometricObject go2){
		return go.findArea() == go2.findArea();
		
	}
	
}
package com.atguigu.exer;

public class GeometricTest {

/*
 * 定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。
 * 定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
 * 编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。

 */
	
	public static void main(String[] args) {
		GeometricTest gt = new GeometricTest();
	    
		Circle c = new Circle(1.0, "red", 1.0);
		Circle c2 = new Circle(1.0, "red", 1.0);
		Myrectangle mr = new Myrectangle(1.0, 2,"blue", 1.5);
		Myrectangle mr2 = new Myrectangle(1.0, 2,"blue", 1.5);
		
		boolean equalsArea = gt.equalsArea(c, c2);
		System.out.println(equalsArea);
		
	    gt.displayGeometricObject(mr);
	}
	
	/**
	 * 显示面积
	 * @param go
	 */
	private void displayGeometricObject(GeometricObject go) {
				System.out.println(	go.findArea());
				}
	
	/**
	 * 比较面积是否相等
	 * @param go
	 * @param go2
	 * @return
	 */
	public boolean equalsArea(GeometricObject go,GeometricObject go2){
		return go.findArea() == go2.findArea();
		
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值