应该这样写:
class Box{
double width ;
double heigth ;
double depth ;
void volume(){
System.out.print("volume is ") ;
System.out.println(width * heigth * depth) ;
}
void setDim(double wid,double hei,double depth){
width = wid;
heigth = hei ;
depth = depth ;
}
}class BoxDemo{
public static void main(String[] lianga){
Box mybox = new Box() ;
mybox.width = 10;
mybox.heigth = 20;
mybox.depth = 30;
// mybox.setDim(10,20,30) ;
mybox.volume();
}
}
class Box{
double width ;
double heigth ;
double depth ;
void volume(){
System.out.print("volume is ") ;
System.out.println(width * heigth * depth) ;
}
void setDim(double wid,double hei,double depth){
width = wid;
heigth = hei ;
depth = depth ;
}
}class BoxDemo{
public static void main(String[] lianga){
Box mybox = new Box() ;
mybox.width = 10;
mybox.heigth = 20;
mybox.depth = 30;
// mybox.setDim(10,20,30) ;
mybox.volume();
}
}
解决方案 »
- java 两个项目对接咋办有没有相关资料啊
- 哪位帮忙翻译成JAVA的代码? 谢谢啊
- 写一个ftp下载,用到了enterprisedt的包,对于正在上传的文件怎么判断
- 使用 request.getRequestDispatcher(url).include(request,response); 可以弹出一个新窗口么!?
- 构造器(构造函数)的作用!!!!!!
- 怎么实现,自动添加表格行数的功能?
- JDBC
- 求助:2个JPanel之间的数据传输
- 可以使用vb编写的*.dll吗
- ms平台下sqlserver的jdbc驱动在哪下载,它的连接字符串如何写
- 这是什么错误 ,我怎么老碰到它呀
- 救命!!!中文显示的问题,我用apache.poi生成excel,但是不能显示中文,各位大哥大姐,就我一命,一定给分。
一个很简单的问题,却很难搞明白到底是为什么。
double widt;
double heigt;
double dept;
public Box(){}
void volume(){
System.out.print("volume is ");
double volume = width * heigth * depth;
System.out.println(volume) ;
}
void setDim(double wid,double hei,double depth){
widt = wid;
heigt = hei ;
dept = depth ;
}
}
volume()中,没有使用这三个赋了值的变量,而是使用了在类Box中的变量,因为这几个没有赋值,所以运算出的结果不是理想中的结果
width = wid;
heigth = hei ;
depth = depth ;
你的上面的double depth的depth换个名字就可以了!因为一般默认情况下初始化数据为0
this.depth = depth;就可以了。