//将继承,多态,重写的性质运用一下
package duotai;
public class GeometricObject {
protected String color;
protected double weight;
protected GeometricObject(String color, double weight) {
super();
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;
}
}
package duotai;
public class Circle extends GeometricObject {
private double radius;
public Circle(String color, double weight, double radius) {
super(color, weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){//重写
return Math.PI*radius*radius;
}
}
package duotai;
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.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;
}
}
package duotai;
public class TestGeometric {
public static void main(String[] args) {
TestGeometric t=new TestGeometric();
Circle c1=new Circle("red", 3.2, 1.2);
Circle c2=new Circle("black", 2.2, 6.2);
MyRectangle m=new MyRectangle(2.3,1.2,"green",2.0);
t.displayGeometricObject(m);
boolean b=t.equalsArea(c1,c2);
System.out.println(b);
}
public boolean equalsArea(GeometricObject o1,GeometricObject o2){//参数类型GeometricObject
// if(o1.findArea()==o2.findArea())
// return true;
// else
// return false;
return o1.findArea()==o2.findArea();
}
public void displayGeometricObject(GeometricObject o){//GeometricObject o=("red", 3.2, 1.2)这就是多态
System.out.println(o.findArea());
}
}