就是这个程序:
class Box
{
double width=10;
double height=10;
double depth=10;
Box()
{
System.out.println("Constructing Box");
}
double volume()
{
return width*height*depth;
}
}
class BoxDemo6
{
public static void main(String []args)
{
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
vol=mybox1.volume();
System.out.println("Volume is"+vol);
vol=mybox2.volume();
System.out.println("Volume is "+vol );
}
}
这样编译能通过,结果:
Constructing Box
Constructing Box
Volume is1000.0
Volume is 1000.0
Press any key to continue...
但是,把这个程序改一下:
class Box
{
double width;
double height;
double depth;
width=10;height=10;depth=10;
Box()
{
System.out.println("Constructing Box");
}
double volume()
{
return width*height*depth;
}
}
class BoxDemo6
{
public static void main(String []args)
{
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
vol=mybox1.volume();
System.out.println("Volume is"+vol);
vol=mybox2.volume();
System.out.println("Volume is "+vol );
}
}
编译就不能通过了,错误如下:
BoxDemo6.java:6: <identifier> expected
width=10;height=10;depth=10;
^
BoxDemo6.java:6: <identifier> expected
width=10;height=10;depth=10;
^
BoxDemo6.java:6: <identifier> expected
width=10;height=10;depth=10;
^
3 errors
谁知道这是怎么回事吗?谢谢!!