java—如何在另一个类中设置和获取内部类的字段

ltskdhd1  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(402)
package com.pr.trio;

import java.util.List;

public class lalala {

     private List<SegmentationFieldValue> segmentationFieldValues;

       public static class SegmentationFieldValue {
          private Integer segmentationFieldId;
          private Integer segmentationFieldGroupId;
          private String value;
        public Integer getSegmentationFieldId() {
            return segmentationFieldId;
        }
        public void setSegmentationFieldId(Integer segmentationFieldId) {
            this.segmentationFieldId = segmentationFieldId;
        }
        public Integer getSegmentationFieldGroupId() {
            return segmentationFieldGroupId;
        }
        public void setSegmentationFieldGroupId(Integer segmentationFieldGroupId) {
            this.segmentationFieldGroupId = segmentationFieldGroupId;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
       }

    public List<SegmentationFieldValue> getSegmentationFieldValues() {
        return segmentationFieldValues;
    }

    public void setSegmentationFieldValues(List<SegmentationFieldValue> segmentationFieldValues) {
        this.segmentationFieldValues = segmentationFieldValues;
    }

}
package com.pr.trio;

import java.util.Arrays;

public class kk {

    public static void main(String[] args) {

        lalala l1 = new lalala();

        //currently passed as an empty array, want to set SegmentationFieldId & value here from inner class
        l1.setSegmentationFieldValues(Arrays.asList());  

        //lalala.SegmentationFieldValue.this.setSegmentationFieldId(15);

        System.out.println(l1.getSegmentationFieldValues());

}
}

所以在这里,我不能为segmentation字段而不是空数组传递值,这是一个错误。那么如何设置内部类字段的值并将其传递到我的列表中呢?

kuarbcqp

kuarbcqp1#

视为你的 SegmentationFieldValue 班级是 public ,在另一个类中使用它很简单,基本上有两种方法:
首先是导入内部类:

import com.pr.trio.lalala.SegmentationFieldValue;

第二种方法是在使用类名时对其进行限定:

lalala.SegmentationFieldValue a = new lalala.SegmentationFieldValue();

然后可以调用此类上的setter,并使用调用中的对象 setSegmentationFieldValues :

lalala.SegmentationFieldValue a = new lalala.SegmentationFieldValue();
a.setSegmentationFieldId(1);
a.setSegmentationFieldGroupId(1);
a.setValue("a");

lalala.SegmentationFieldValue b = new lalala.SegmentationFieldValue();
b.setSegmentationFieldId(2);
b.setSegmentationFieldGroupId(1);
b.setValue("b");

l1.setSegmentationFieldValues(Arrays.asList(a, b));

从您的注解代码来看,您似乎也在寻找一种将元素添加到列表中的速记方法。一个简单的实现可以像这样(在类中 lalala ):

public void addSegmentationFieldValue(Integer id, Integer groupId, String value)
{
    if (segmentationFieldValues == null)
    {
        segmentationFieldValues = new ArrayList<>();
    }

    SegmentationFieldValue result = new SegmentationFieldValue();
    result.setSegmentationFieldId(id);
    result.setSegmentationFieldGroupId(groupId);
    result.setValue(value);
    segmentationFieldValues.add(result);
}

之后,您可以在 main 方法 k1 :

l1.addSegmentationFieldValue(1, 1, "a");

相关问题