设计一个几何类 GeometricObject.java,设计一个圆类Circle.java 是几何类的子类,设计一个矩形类 Rectangle.java 是几何类的子类,设计一个测试程序,创建2个圆对象,2个矩形对象,2个三角形对象,付给GeometricObject的对象数组。用一个循环输出6个对象的名称,相关属性和面积,周长(用toString实现)。
GeometricObject.java
package ex3;
import java.util.Date;
publicabstractclass GeometricObject {
private String color;
privatebooleanisFilled;
private Date dataCreated;
public GeometricObject() {
this("white",false);
}
public GeometricObject(String color, booleanisFilled) {
this.color = color;
this.isFilled = isFilled;
this.dataCreated = dataCreated;
}
public String getColor() {
returncolor;
}
publicvoid setColor(String color) {
this.color = color;
}
publicboolean isFilled() {
returnisFilled;
}
publicvoid setFilled(booleanisFilled) {
this.isFilled = isFilled;
}
public Date getDataCreated() {
returndataCreated;
}
publicvoid setDataCreated(Date dataCreated) {
this.dataCreated = dataCreated;
}
publicabstractdouble getArea();
publicabstractdouble getPerimeter();
@Override
public String toString() {
return"GeometricObject[color=" + color + ",isFilled=" + isFilled
+ ",dataCreated=" + dataCreated + "]";
}
}
Circle.java
package ex3;
/**
* 几何类的子类
* @author cls
*
*/
publicclass Circle extends GeometricObject{
privatedoubleradius;
public Circle(doubleradius) {
super();
this.radius = radius;
}
public Circle(String color,booleanisFilled,doubleradius){
super(color,isFilled);
}
public Circle(){
//this(1.0);
super();
this.radius=1.0;
}
publicdouble getRadius() {
returnradius;
}
publicvoid setRadius(doubleradius) {
this.radius = radius;
}
publicdouble getArea(){
return Math.PI*Math.pow(radius, 2);
}
publicdouble getPerimeter(){
return Math.PI*radius*2;
}
publicdouble getDiameter(){
return 2*radius;
}
@Override
public String toString() {
return"Circle[radius=" + radius + ",getRadius()=" + getRadius()
+ ",getArea()=" + getArea() + ", getPerimeter()="
+ getPerimeter() + ", getDiameter()=" + getDiameter() + "]";
}
Rectangle.java
package ex3;
publicclass Rectengle extends GeometricObject{
privatedoublewidth;
privatedoubleheight;
public Rectengle(doublewidth, doubleheight) {
super();
this.width = width;
this.height = height;
}
public Rectengle() {
super();
this.width = 1;
this.height = 1;
}
public Rectengle(String color, booleanisFilled,doublewidth,doubleheight) {
super(color, isFilled);
// TODO Auto-generatedconstructor stub
}
publicdouble getWidth() {
returnwidth;
}
publicvoid setWidth(doublewidth) {
this.width = width;
}
publicdouble getHeight() {
returnheight;
}
publicvoid setHeight(doubleheight) {
this.height = height;
}
publicdouble getArea(){
returnthis.height*this.width;
}
publicdouble getPerimeter(){
return (this.height+this.width)*2;
}
@Override
public String toString() {
return"Rectengle[width=" + width + ",height=" + height
+ ", getWidth()=" + getWidth() + ",getHeight()=" + getHeight()
+ ",getArea()=" + getArea() + ", getPerimeter()="
+ getPerimeter() + "]";
}
}
Triangle.java
package ex3;
publicclass Triangle extends GeometricObject{
privatedoubleside1,side2,side3;
public Triangle(doubleside1,doubleside2,doubleside3){
super();
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public Triangle() {
super();
this.side1=1.0;
this.side2=1.0;
this.side3=1.0;
// TODO Auto-generatedconstructor stub
}
public Triangle(String color,booleanisFilled,doubleside1,doubleside2,doubleside3){
super(color,isFilled);
}
publicdouble getSide1() {
returnside1;
}
publicvoid setSide1(doubleside1) {
this.side1 = side1;
}
publicdouble getSide2() {
returnside2;
}
publicvoid setSide2(doubleside2) {
this.side2 = side2;
}
publicdouble getSide3() {
returnside3;
}
publicvoid setSide3(doubleside3) {
this.side3 = side3;
}
publicdouble getArea(){
doubles=(this.side1+this.side2+this.side3)/2;
return Math.sqrt(s*(s-this.side1)*(s-this.side2)*(s-this.side3));
}
publicdouble getPerimeter(){
returnthis.side1+this.side2+this.side3;
}
@Override
public String toString() {
return"Triangle[side1=" + side1 + ", side2=" + side2 + ",side3="
+ side3 + ",getSide1()=" + getSide1() + ", getSide2()="
+ getSide2() + ", getSide3()=" + getSide3() + ", getArea()="
+ getArea() + ",getPerimeter()=" + getPerimeter() +"]";
}
}
TestGeometricObject.java
package ex3;
publicclass testGeometric {
publicstaticvoid main(String[] args) {
// TODO Auto-generatedmethod stub
//GeometricObject o1 = newGeometricObject();
//System.out.println(o1.toString());
/*Circle c1=new Circle();
System.out.println(c1.toString());
Rectengle r1 = new Rectengle();
System.out.println(r1.toString());
Triangle t1 = new Triangle();
System.out.println(t1.toString());*/
GeometricObject[] olist = new GeometricObject[6];
olist[0]=new Circle();
olist[1]=new Circle(5.0);
olist[2]=new Rectengle();
olist[3]=new Rectengle(2,5);
olist[4]=new Triangle();
olist[5]=new Triangle(3,4,5);
for(inti=0;i<6;i++){
System.out.println(olist[i].toString());
}
}
}
总结:通过本列掌握了继承与多态的概念,以及子类和父类程序的设计具体语法