在另一个 class 中访问一个 class 的实例时出现问题

Issues in accessing the instances of one class within other class

我正在尝试使用一个 class 在另一个 class 中的方法访问实例并尝试修改它们。但它向我抛出错误,因为“无法引用在封闭 scope.And 中定义的非最终局部变量 nextSourceOffset 它给了我一个将变量修改为 final 的修复方法".但是如果我把它改成final,我就不能递增它们了。

我正在使用以下代码:

public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker)
            throws StageException {
        long nextSourceOffset = 0;
        if (lastSourceOffset != null) {
            nextSourceOffset = Long.parseLong(lastSourceOffset);
        }
        final RtmClient client = new RtmClientBuilder(getEndPoint(), getAppKey()).setListener(new RtmClientAdapter() {
            @Override
            public void onEnterConnected(RtmClient client) {

            }
        }).build();
        SubscriptionAdapter listener = new SubscriptionAdapter() {

            @Override
            public void onSubscriptionData(SubscriptionData data) {
                int numRecords = 0;
                for (AnyJson json : data.getMessages()) {
                    jsonString = json.toString();
                    Record record = getContext().createRecord("some-id::" + nextSourceOffset);
                    Map<String, Field> map = new HashMap<>();
                    map.put("fieldName", Field.create(jsonString));
                    record.set(Field.create(map));
                    batchMaker.addRecord(record);
                    ++nextSourceOffset;
                    ++numRecords;

                }
            }

        };

它在以下位置抛出错误:

Record record = getContext().createRecord("some-id::" + nextSourceOffset);//error 
                    Map<String, Field> map = new HashMap<>();
                    map.put("fieldName", Field.create(jsonString));
                    record.set(Field.create(map));
                    batchMaker.addRecord(record);//error
                    ++nextSourceOffset;//error
                    ++numRecords;//error

您的内部 class 无法从外部范围访问非最终变量。它也不能改变变量的值。

最干净的解决方案可能是将所有这些信息封装在一个新的 class 中并将其存储为属性。

快速而简单的解决方案是将本地 long 变量转换为 long 类型的单个元素数组。变量本身将是最终的,因为它总是引用同一个数组,但数组的内容仍然可以更改。

示例:

 public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker)
        throws StageException {
    long[] nextSourceOffset = {0};
    if (lastSourceOffset != null) {
        nextSourceOffset[0] = Long.parseLong(lastSourceOffset);
    }
    final RtmClient client = new RtmClientBuilder(getEndPoint(), getAppKey()).setListener(new RtmClientAdapter() {
        @Override
        public void onEnterConnected(RtmClient client) {

        }
    }).build();
    SubscriptionAdapter listener = new SubscriptionAdapter() {

        @Override
        public void onSubscriptionData(SubscriptionData data) {
            int numRecords = 0;
            for (AnyJson json : data.getMessages()) {
                jsonString = json.toString();
                Record record = getContext().createRecord("some-id::" + nextSourceOffset[0]);
                Map<String, Field> map = new HashMap<>();
                map.put("fieldName", Field.create(jsonString));
                record.set(Field.create(map));
                batchMaker.addRecord(record);
                ++nextSourceOffset[0];
                ++numRecords;

            }
        }

    };