第1个回答 2011-04-11
abstract class Area{
abstract double area();
}
//矩形面积类
class RectArea extends Area{
private double width; //宽
private double height; //高
public RectArea(){
}
public RectArea(double width, double height){
this.width = width;
this.height = height;
}
public double area() {
return this.width * this.height;
}
public double getWidth(){
return this.width;
}
public void setWidth(double width){
this.width = width;
}
public double getHeight(){
return this.height;
}
public void setHeight(double height){
this.height = height;
}
}
//圆面积类
class RoundArea extends Area{
private double radius; //半径
public RoundArea(){
}
public RoundArea(double radius){
this.radius = radius;
}
public double area() {
return Math.PI * this.radius * this.radius;
}
public double getRadius(){
return this.radius;
}
public void setRadius(double radius){
this.radius = radius;
}
}
class ImpleArea{
public void displayArea(){
Scanner scan = new Scanner(System.in);
Area a;
a = new RectArea();
System.out.print("矩形的宽:");
((RectArea)a).setWidth(scan.nextDouble());
System.out.print("矩形的高:");
((RectArea)a).setHeight(scan.nextDouble());
System.out.println("矩形的面积:" + a.area());
a = new RoundArea();
System.out.print("圆的半径:");
((RoundArea)a).setRadius(scan.nextDouble());
System.out.println("圆的面积:" + a.area());
}
}
public class Test {
public static void main(String[] args) {
ImpleArea impl = new ImpleArea();
impl.displayArea();
}
}