当类构造函数具有参数时,如何在Java中初始化数组? [英] How to initialize array in java when the class constructor has parameters?
本文介绍了当类构造函数具有参数时,如何在Java中初始化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个类的构造函数:
I have this class constructor:
public Category(int max){
...
}
问题是,我想创建一个此类的数组,如何初始化它?
The thing is, I want to make an array of this class, how do I initialize it?
private Category categories = new Category(max)[4];
不起作用.
UPDATE
UPDATE
我需要做这样的事情吗?
Do I need to do something like this?
private Category[] categories = new Category[4];
然后初始化每个对象?
推荐答案
在创建数组时,您正在创建Category数组.那是数组的一个实例.
When you are making an array , you are creating an array of Category. That s an instance of array.
在使用Category对象填充数组时,此时将使用带有Const的Category.
When you are populating the array with Category objects, at that point you use the Category with Const.
Category [] categories = new Category[4];
categories[0] = new Category(10);
这篇关于当类构造函数具有参数时,如何在Java中初始化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文