1、几何类
package test3;
/**
* 〈一句话功能简述〉<br>
* 〈父类几何形状〉
*
* @author abu
* @create 2019/7/15
* @since 1.0.0
*/
public class GeometricObject {
protected String color;
protected double weight;
public GeometricObject() {
}
public GeometricObject(String color, double weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double findArea(){
return 0.0;
}
}
2、圆类
package test3;
/**
* 〈一句话功能简述〉<br>
* 〈圆〉
*
* @author abu
* @create 2019/7/13
* @since 1.0.0
*/
public class Circle extends GeometricObject {
private double radius;
public Circle(String color, double weight, double radius) {
super(color, weight);
this.radius = radius;
}
// public Circle(){
// radius = 1;
// }
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){
return Math.PI * radius * radius;
}
}
3、矩形类
package test3;
/**
* 〈一句话功能简述〉<br>
* 〈子类矩形〉
*
* @author abu
* @create 2019/7/15
* @since 1.0.0
*/
public class MyRectangle extends GeometricObject {
protected double width;
protected double height;
public MyRectangle(String color, double weight, double width,
double height) {
super(color, weight);
this.width = width;
this.height = height;
}
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;
}
public double findArea(){
return width * height;
}
}
4、测试类
package test3;
/**
* 〈一句话功能简述〉<br>
* 〈测试不同子类的面积〉
*
* @author abu
* @create 2019/7/15
* @since 1.0.0
*/
public class TestEqualArea {
public static void main(String[] args){
TestEqualArea t = new TestEqualArea();
Circle c1 = new Circle("red", 2, 3);
Circle c2 = new Circle("green", 3,4);
MyRectangle m1 = new MyRectangle("white",2,4,4);
t.displayGeometricObject(c1);
t.displayGeometricObject(m1);
boolean b = t.equalsArea(c1,m1);
System.out.println(b);
}
public boolean equalsArea(GeometricObject o1, GeometricObject o2){
// if(o1.findArea() == o2.findArea())
// return true;
// else
// return false;
return o1.findArea() == o2.findArea();
}
public void displayGeometricObject(GeometricObject o){
System.out.println(o.findArea());
}
}