package aaa;
public class ccc {
public static void main(String[] args) {
// TODO Auto-generated method stub
MagicBox box=new MagicBox(length:50,width:60,heigth20);
System.out.println("length:"+box.length);
System.out.println("width:"+box.width);
System.out.println("heigth:"+box.heigth);
}
}
class MagicBox{
public int length;
public int width;
public int heigth;
public MagicBox( int length,int width,int heigth) {
this.length=length;
this.width=width;
this.heigth=heigth;
}
}
在 Java 中,创建对象时,括号中的参数用于传递给构造函数。在你的代码中,构造函数需要传递三个参数:length、width 和 heigth。然而,当你创建 MagicBox 对象时,你在传递参数时写错了:
MagicBox box = new MagicBox(length:50, width:60, heigth20);
这应该写成:
MagicBox box = new MagicBox(50, 60, 20);
这样就可以正确创建 MagicBox 对象并传递正确的参数。答案参考来自 https://www.wodianping.com/