interface NewShape{
public void setName(String string);
public void setArea(double area);
public String getName();
public double getArea();
public double getCir();//计算周长
}
class NewRectangle implements NewShape{
String name;
double length;
double width;
double area;
NewRectangle(double length,double width){
this.length = length;
this.width = width;
}
public void setName(String name){
this.name = name;
}
public void setArea(double area){
this.area = area;
}
public String getName(){
return name;
}
public double getArea(){
computeArea();
return area;
}
public double getCir(){
return 2*(length+width);
}
void computeArea(){
area = length * width;
}
}
class NewCircle implements NewShape{
static double PI = 3.14;
String name;
double radius;
double area;
NewCircle(double radius){
this.radius = radius;
}
public void setName(String name){
this.name = name;
}
public void setArea(double area){
this.area = area;
}
public String getName(){
return name;
}
public double getArea(){
computeArea();
return area;
}
public double getCir(){
return 2*PI*radius;
}
void computeArea(){
area = PI * radius * radius;
}
}
public class CalArea {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NewShape rec = new NewRectangle(12.4,3.6);
rec.setName("方形");
NewShape cir = new NewCircle(7.5);
cir.setName("圆形");
System.out.println(rec.getName()+"面积:"+rec.getArea()+"t周长:"+rec.getCir());
System.out.println(cir.getName()+"面积:"+cir.getArea()+"t周长:"+cir.getCir());
}
}
大神都不做这么简单的题,只能我献丑了~