Java 如何创建现有类的新实例?
我正在用Java编写一个项目,并用以下代码创建了一个类:Java 如何创建现有类的新实例?,java,class,instance,Java,Class,Instance,我正在用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 mi
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告诉我:
必需:(int[],int,int,int,int[]
已找到:(int[],int[],int[],int[],int[]
我错过了什么?我显然没有将这些变量初始化为int[],那么为什么要将它们作为int[]来提取呢?您不能初始化像
int
,boolean
,char
,float
,double
等简单的数据类型。
将其更改为类似的内容:
VehInfo vehinfo = new VehInfo(new int[20][150], 0, 0, new int[5]);
或者删除传递的属性。在我看来,您设置了默认值:
private int mintime = 0;
private int numvehicles = 1;
不能启动简单的数据类型,如
int
、boolean
、char
、float
、double
等。
将其更改为类似的内容:
VehInfo vehinfo = new VehInfo(new int[20][150], 0, 0, new int[5]);
或者删除传递的属性。在我看来,您设置了默认值:
private int mintime = 0;
private int numvehicles = 1;
试一试
编辑:太晚了:(试试看
编辑:太晚了:(您的构造函数
public VehInfo(int[][] traffic, int mintime, int numvehicles, int[] vehiclecode)
需要这样的情况。假设您已声明变量:
int[][] traffic = new int[20][150];
int mintime = 0;
int numvehicles = 0;
int[] vehiclecode = new int[5];
然后可以为它们指定值:
/** here in your program you assign some values */
mintime = 30;
numvehicles = 2;
vehiclecode[0] = 44;
vehiclecode[1] = 77;
然后创建VehInfo类的对象
/** then in you program you can create vehinfo objects */
VehInfo vehinfo = new VehInfo(traffic, mintime, numvehicles, vehiclecode);
你的构造器
public VehInfo(int[][] traffic, int mintime, int numvehicles, int[] vehiclecode)
需要这样的情况。假设您已声明变量:
int[][] traffic = new int[20][150];
int mintime = 0;
int numvehicles = 0;
int[] vehiclecode = new int[5];
然后可以为它们指定值:
/** here in your program you assign some values */
mintime = 30;
numvehicles = 2;
vehiclecode[0] = 44;
vehiclecode[1] = 77;
然后创建VehInfo类的对象
/** then in you program you can create vehinfo objects */
VehInfo vehinfo = new VehInfo(traffic, mintime, numvehicles, vehiclecode);
您正在努力解决语法错误
试试这个
VehInfo-VehInfo=new-VehInfo(new-int[20][150],1,2,new-int[5]);
您正在努力解决语法错误
试试这个VehInfo-VehInfo=new-VehInfo(new-int[20][150],1,2,new-int[5]);
这样做:
VehInfo vehinfo1 = new VehInfo(new int[20][150], new Integer(1), new Integer(1), new int[5]);
或者这个:
VehInfo vehinfo2 = new VehInfo(new int[20][150], 1, 1, new int[5]);
因为int(或Integer)是原语,所以它们不能在没有值的情况下实例化。按如下方式执行:
VehInfo vehinfo1 = new VehInfo(new int[20][150], new Integer(1), new Integer(1), new int[5]);
或者这个:
VehInfo vehinfo2 = new VehInfo(new int[20][150], 1, 1, new int[5]);
因为int(或Integer)是原语,所以在没有值的情况下无法实例化它们。尝试使用数字而不是像这样的“new int”初始化类
VehInfo vehinfo = new VehInfo(new int[20][150], 0, 0, new int[5]);
尝试使用数字而不是像这样的“newint”初始化类
VehInfo vehinfo = new VehInfo(new int[20][150], 0, 0, new int[5]);
尝试:
您需要为参数提供实际值,以便使用您创建的VehInfo构造函数初始化VehInfo类型的对象:
int[][] myint1 = new int[5][5]; //Allocating memory for 5x5 array
myint1 = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
VehInfo myVehInfo = new VehInfo(myint1, 5, 5, int[5]);
尝试:
您需要为参数提供实际值,以便使用您创建的VehInfo构造函数初始化VehInfo类型的对象:
int[][] myint1 = new int[5][5]; //Allocating memory for 5x5 array
myint1 = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
VehInfo myVehInfo = new VehInfo(myint1, 5, 5, int[5]);
你认为“new int”能完成什么?new int应该被int替换为which int?new int的值是什么?你认为“new int”能完成什么?new int应该被int替换为which int?new int的值是什么?