初始化数组中的对象

用户名

我已经在数组中玩了一段时间了,这个问题一直困扰着我。

我创建了一个用户定义的对象,并在这样的数组中声明了它:`Property regesteredAssets [] = new Property [200];

这是我的构造函数:

public Property(String newPropertyName,String newPropertyAddress,String newPropertyType, String newPropertyDescription)
    {

    propertyName[arraySequence] = newPropertyName;
    propertyFullAddress[arraySequence] = newPropertyAddress;
    propertyType[arraySequence] = newPropertyType;
    propertyDescription[arraySequence] = newPropertyDescription;

        arraySequence++;



}

我想regesteredAsssets[]根据自己的意愿初始化每个数组我该怎么做?我也必须在Property类的属性中使用数组吗?

罗伯托

您不需要将属性设置为数组,除非特定资产具有多个属性。在这种情况下,我认为不会。您可以极大地简化代码,如下所示:

public class Property {
    private String name, address, type, description;

    public Property(String name, String address, String type, String description) {
        this.name = name;
        this.address = address;
        this.type = type;
        this.description = description;
    }

    public static void main(String[] args) {
        Property[] registeredAssets = new Property[200];

        registeredAssets[0] = new Property("Joe Bloggs", "555 Fake St.", "IMPORTANT", "Lorem Ipsum Dolor");
        // etc.
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章