2)内部设计
Circle类和Rectangle类中覆盖了定义在GeometricObject类中的getArea()和getPerimeter()方法。语句: GeometricObject geoObject1=new Circle(5);
GeometricObject geoObject2=new Rectangle(5,3);
创建了一个新圆和一个新矩形,并把它们赋值给变量geoObject1和geoObject2。这两个变量都是GeometricObject类型的。
当调用equalArea(geoObject1,geoObject2)时,由于geoObject1是一个圆,所以object1.getArea()使用的是Circle类定义的getArea()方法,而geoObject2是一个矩形,所以object2.getArea()使用的是Rectangle类的getArea()方法。
类似地,当调用displayGeometricObject(geoObject1),使用在Circle类中定义的getArea和getPerimeter方法,而当调用displayGeometricObject(geoObject2)时,使用的是在Rectangle类中定义的getArea和getPerimeter方法。 六、完整源码
实现Comparable接口的Circle类
a、 运用继承和多态
1)GeometricObject.Java
package yuan1;
public class GeometricObject {
private String color =\; private boolean filled;
private java.util.Date dateCreated; public GeometricObject(){ }
public GeometricObject(String color,boolean filled){
11
dateCreated = new java.util.Date();
}
}
dateCreated = new java.util.Date(); this.color = color; this.filled = filled;
public String getColor(){ }
public void setColor(String color){ }
public boolean isFilled(){ }
public void setFilled(boolean filled){ }
public java.util.Date getDateCreated(){ }
public String toString(){ }
return\+dateCreated+\+color+\+filled; return dateCreated; this.filled = filled; return filled; this.color = color; return color;
2) Circle.java
package yuan1;
public class Circle extends GeometricObject{
private double radius; public Circle(){ }
public Circle(double radius){ }
public Circle(double radius,String color,boolean filled){ }
public double getRadius(){ }
public void setRandius(double radius){ }
12
this.radius =radius;
this.radius =radius; setColor(color); setFilled(filled);
return radius;
this.radius =radius;
}
public double getArea(){ }
public double getDiameter(){ }
public double getPerimeter(){ }
public void printCircle(){ }
System.out.println(\+getDateCreated()+\return 2*radius*Math.PI; return 2*radius;
return radius*radius*Math.PI;
radius is\+radius);
3) Rectangle.java
package yuan1;
public class Rectangle extends GeometricObject{
private double width; private double height; public Rectangle(){ }
public Rectangle(double width,double height){ }
public Rectangle(double width,double height,String color,boolean filled){ }
public double getWidth(){ }
public void setWidth(double width){ }
public double getHeight(){ }
public void setHeight(double height){ }
13
this.width=width; this.height=height;
this.width=width; this.height=height; setColor(color); setFilled(filled);
return width;
this.width=width;
return height;
this.height=height;
}
public double getArea(){ }
public double getPerimeter(){ }
return 2*(width+height); return width*height;
4) TestCircleRectangle.java
package yuan1;
public class TestCircleRectangle { }
public static void main(String[]args){ }
Circle circle=new Circle(1);
System.out.println(\+circle.toString()); System.out.println(\+circle.getRadius()); System.out.println(\+circle.getArea());
System.out.println(\+circle.getDiameter()); Rectangle rectangle=new Rectangle(2,4);
System.out.println(\+rectangle.toString()); System.out.println(\+rectangle.getArea());
System.out.println(\+rectangle.getPerimeter());
a、 运用继承和多态
1)GeometricObject.Java
package yuan;
public abstract class GeometricObject {
private String color =\; private boolean filled;
private java.util.Date dateCreated; protected GeometricObject(){ }
protected GeometricObject(String color,boolean filled){ }
public String getColor(){ }
public void setColor(String color){
14
dateCreated = new java.util.Date();
dateCreated = new java.util.Date(); this.color = color; this.filled = filled;
return color;
}
}
this.color = color;
public boolean isFilled(){ }
public void setFilled(boolean filled){ }
public java.util.Date getDateCreated(){ }
public String toString(){ }
public abstract double getArea(); public abstract double getPerimeter();
return\+dateCreated+\+color+\+filled; return dateCreated; this.filled = filled; return filled;
2) Circle.java
package yuan;
public class Circle extends GeometricObject{
private double radius; public Circle(){ }
public Circle(double radius){ }
public Circle(double radius,String color,boolean filled){ }
public double getRadius(){ }
public void setRandius(double radius){ }
public double getArea(){ }
public double getDiameter(){ }
15
this.radius =radius;
this.radius =radius; setColor(color); setFilled(filled);
return radius;
this.radius =radius;
return radius*radius*Math.PI;
return 2*radius;