这个程序不出 结果
有异常
可是我不知道怎么改
一个就是求长方形的面积什么的
的二个是继承
然后重写父类的某些方法
public class MyRectangle {
private int rxUp,ryUp,lxUp,lyUp;
int width;
int height;
int area1;
public MyRectangle(int rxUp,int ryUp,int lxUp,int lyUp){
this.lxUp=lxUp;
this.lyUp=lyUp;
this.rxUp=rxUp;
this.ryUp=ryUp;
}//构造方法
public int getW(){
width=lyUp-ryUp;
return width;
}
public int getH(){
height=rxUp-lxUp;
return height;
}
public int area(int width,int height){
this.getH();
this.getW();
area1=height*width;
return area1;
}
public String toString(){
return "宽:"+width+" 长:"+height+" 面积:"+area1;
}
/*public int getrxUp(){
return rxUp;
}
public int getlxUp(){
return lxUp;
}
public int getryUp(){
return ryUp;
}
public int getlyUp(){
return lyUp;
}*/
public static void main(String args[]){
MyRectangle a= new MyRectangle(20,5,10,20);
a.area(a.getH(),a.getW());
System.out.println(a.toString());
}
}
public class MyCube extends MyRectangle{
MyRectangle mian;
int d;
long areac;
long vol;
public MyCube(int rxUp,int ryUp,int lxUp,int lyUp,int f){
super(rxUp,ryUp,lxUp,lyUp);
this.d=f;
}
public long volume(){
vol=mian.width*mian.height*d;
return vol;
}
public long cubearea(){
areac=2*(super.area(mian.width,mian.height)+super.area(mian.width,d)+super.area(d,mian.height));
return areac;
}
public static void main(String args[]){
try{
MyCube mc=new MyCube(20,5,10,20,6);
//mc.getH();
//mc.getW();
System.out.println("立方体的表面积:"+mc.cubearea());
System.out.println("立方体的体积:"+mc.volume());
}catch(NullPointerException ex){
//System.out.println("the error is"+ex);
//ex.printStackTrace();
System.out.println("the error is:"+ex.getMessage());
}
}
}