日期:2014-05-18  浏览次数:20680 次

Comparable接口 比较圆形和长方形面积
三个类:一个 Cricle        Rectangle       Geometric  还一个主方法 Main  
Cricle        
--------------------------------------

 class  P_372_Cricle  extends P_372_Geometric implements Comparable
{
private double Radius;

public P_372_Cricle()
{

}
 
public P_372_Cricle(double radius)
{
  this.Radius = radius;
}

public P_372_Cricle(String color , boolean fillde , double radius)
{
super(color , fillde);
this.Radius = radius;
}

public void setRadius(double radius)
{
this.Radius = radius;
}

public double getRadius()
{
return this.Radius;


public double getdiameter()
{
return 2 * getRadius();
}

public   double getArea()
{
return this.getRadius() * this.getRadius() * Math.PI;
}

public  double getPerimeter()
{
return this.getRadius() * 2 * Math.PI;
}

@Override
public int compareTo(Object o)
{
  if(this.getArea() > ((P_372_Cricle)o).getArea())
return 1;
else if(this.getArea() < ((P_372_Cricle)o).getArea())
return -1;
else
return 0;
}

public String toString()
{
return "圆的面积是:" + getArea() + "圆形面积大";
}
}

-------Rectangle       

class P_372_Rectangle extends P_372_Geometric implements Comparable
{
private double weigh;
private double heigh;

public P_372_Rectangle()
{

}

public P_372_Rectangle(double weigh , double heigh)
{
this.weigh = weigh;
this.heigh = heigh;
}

public P_372_Rectangle(double weigh , double heigh , String color , boolean filled)
{
super(color, filled);
this.weigh = weigh;
this.heigh = heigh; 
}

public double getWight()
{
return this.weigh;
}

public void setWight(double wight)
{
this.weigh = wight;
}

public double getHeigh()
{
return this.heigh;
}

public void setHight(double heigh)
{
this.heigh =heigh;
}
 
public  double getArea()
{
return this.getHeigh() * this.getWight();
}
 
public  double getPerimeter()
{
return (this.getHeigh() + this.getWight()) * 2;
}

public String toString()
{
return "正方形的面积是:" + getArea() + "正方形面积大";
}
@Override
public int compareTo(Object o)
{

P_372_Rectangle&nbs