星辰xyz 2023-03-13 11:56 采纳率: 50%
浏览 19
已结题

创建box对象时,后面括号中的参数为什么会报错

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;
}

}

  • 写回答

1条回答 默认 最新

  • 嗷呜大嘴狼 2023-03-13 12:04
    关注

    在 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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日
  • 创建了问题 3月13日

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真