- 图形类GeometricObject,其定义如下:
GeometricObject |
-name:String(常量值:图形) -color:String |
+ GeometricObject (String) +getColor():String +setColor(String):void +getName():String +toString():String |
package job2;
public class GeometricObject {
private String name;
private String color;
public GeometricObject(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public String toString() {
return name+"的面积为";
}
}
(2)GeometricObject的子类圆类Circle,其定义如下:
Circle |
-name:String(常量值:圆形) -radius:double |
+ Circle (String,double) +getRadius():double +setRadius(double):void +getName():String +getArea():double //求面积 +toString():String |
package job2;
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius,String name) {
super(name);
this.radius=radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius=radius;
}
public double getArea() {
return radius*radius*3.14;
}
public String toString() {
return super.toString();
}
}
(3)GeometricObject的子类矩形类Rectangle,其定义如下:
Rectangle |
-name:String(常量值:矩形) -length:double -width:double |
+Rectangle(String,double,double) +getLength():double +setLength(double):void +getWidth():double +setWidth(double):void +getName():String +getArea():double //求面积 +toString():String |
package job2;
public class Rectangle extends GeometricObject {
private double length;
private double width;
public Rectangle(double length,double width,String name) {
super(name);
this.length=length;
this.width=width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length=length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width=width;
}
public double getArea() {
return length*width;
}
public String toString() {
return super.toString();
}
}
(4)测试类源程序:
package job2;
public class Test {
public static void main(String[] args) {
Circle c1=new Circle(1.5,"圆");
Rectangle r1=new Rectangle(3,5,"矩形");
System.out.println(c1.toString()+c1.getArea());
System.out.println(r1.toString()+r1.getArea());
}
}
(5)结果: