gpt4 book ai didi

java - 如何创建现有类的新实例?

转载 作者:行者123 更新时间:2023-11-29 10:15:04 24 4
gpt4 key购买 nike

我正在用 Java 编写一个项目,并使用以下代码创建了一个类:

public class VehInfo {
private int[][] traffic = new int[20][150];
private int mintime = 0;
private int numvehicles = 1;
private int[] vehiclecode = new int[5];

public VehInfo(int[][] traffic, int mintime, int numvehicles, int[] vehiclecode) {
this.traffic = traffic;
this.mintime = mintime;
this.numvehicles = numvehicles;
this.vehiclecode = vehiclecode;
}
}

我想使用以下代码创建此类的新实例

VehInfo vehinfo = new VehInfo(new int[20][150], new int, new int, new int[5]);

但是,Netbeans 通知我:

required: (int[][], int, int, int[])

found: (int[][], int[], int[], int[])

我错过了什么?我显然没有将这些变量初始化为 int[],那么为什么要这样选择它们呢?

最佳答案

您不能初始化简单的数据类型,例如 intbooleancharfloatdouble 等将其更改为类似的内容:

VehInfo vehinfo = new VehInfo(new int[20][150], 0, 0, new int[5]);

或删除传递的属性。在我看来,您为其设置了默认值:

private int mintime = 0;
private int numvehicles = 1;

关于java - 如何创建现有类的新实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com