java类的继承(父类和接口)
源代码
public abstract class GeometricObject {
private String color=“white”;
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject(){
dateCreated=new java.util.Date();
}
protected GeometricObject(String color,boolean filled){
dateCreated=new java.util.Date();
this.color=color;
this.filled=filled;
}
public String getcolor(){
return color;
}
public void setColor(String color) {
this.color=color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled=filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public String toString(){
return “Created on”+dateCreated+"\ncolor:"+color+“and filled:”+filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
2)Octagon类
public class Octagon extends GeometricObject implements Comparable,Cloneable{
private double x;
public Octagon(String color,boolean filled,double x)
{super(color,filled);
this.x=x;}
public double getArea()
{return (2+4/Math.sqrt(2)Math.pow(x, 2));}
public double getPerimeter()
{return 8x;}
public Object clone() throws CloneNotSupportedException
{return super.clone();}
public int compareTo(Object o)
{if(this==(Octagon)o)
return 1;
else
return 0;}
public String toString(){
return super.toString()+" side:"+x+" Area:"+getArea()+" Perimeter:"+getPerimeter();
}
}
UML图