代码之家  ›  专栏  ›  技术社区  ›  sri

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

  •  -1
  • sri  · 技术社区  · 8 年前

    “。但如果我将其更改为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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   DudeDoesThings    8 年前

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

    例子:

     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;
    
                }
            }
    
        };