class box {
double ht,wdt,len;
box(double h,double w,double l) {
ht=h;
wdt=w;
len=l;
}
double volume() {
return ht*wdt*len;
}
}
class boxme {
public static void main(String args[]) {
box mybox= new box(1,2,3);
System.out.print("The volume is "+mybox.volume());
}
}//对于在bluej中运行的这段代码,我仍然需要在对象创建之后给出参数(虽然我已经在我的代码中给出了参数).The相同的代码在cmd中运行得很好,但在bluej.Please中尝试时显示了这种差异提供了一个理由和解决方案来实现bluej和cmd之间的等价性??//
发布于 2013-03-07 04:58:28
如果您有两个不同的类,并且您希望使用另一个类中的方法,则必须创建该类的一个实例。
右键单击第二个类并运行public static void main(String args[])函数。
请注意,类的名称必须以大写字母开头,并且为安全起见,字段必须为private作用域,对象应始终为小写。
public class Box {
private double ht,wdt,len;
public Box(double h,double w,double l) {
ht=h;
wdt=w;
len=l;
}
public double volume() {
return ht*wdt*len;
}
}
public class boxme {
public static void main(String args[]) {
Box mybox= new Box(1,2,3);
System.out.print("The volume is "+mybox.volume());
}
}发布于 2012-04-15 02:54:01
在BlueJ中显式运行对象时不需要创建对象,因为已经定义了主函数。
右键单击该类并运行public static void main(String args[])函数。
https://stackoverflow.com/questions/10156277
复制相似问题