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();
}
}