本帖最后由 沈艳南 于 2013-5-20 00:50 编辑
public class Test2 {
public static void main(String[] args) {
Shap s[] = new Shap[3];
s[0] =new Rect(8,0);
s[1] =new Rect(5.0,6.0);
s[2] =new Rect(new Rect(12.0,4.0));
for(int i=0;i<s.length;i++){
System.out.println("s["+i+"]area="+s.area());
}
}
}
abstract class Shap{
private double w,h;
Shap(double i){
w=h=i;
}
Shap(double i,double j){
w = i;
h = j;
}
Shap(Shap s){
w = s.w;
h = s.h;
}
double getw(){
return h;
}
double geth(){
return w;
}
abstract double area();
}
class Rect extends Shap{
Rect(double i){
super(i);
}
Rect(double i,double j){
super(i,j);
}
Rect(Rect r){
super(r);
}
double area(){
return getw()*geth();
}
}谁能告诉我为什么s[2]输出的是48?
|