简体   繁体   English

方法中定义的访问接口变量

[英]Access interface variables defined in method

I'll just paste the code then explain what I need. 我将粘贴代码,然后解释我需要什么。 I'm lost as to how to acheive this, and I can't even think of alternatives that fit my code very well. 我对如何实现这一目标一无所知,甚至无法想到完全适合我的代码的替代方法。

public Transform findById(final int id) {
    final Transform returnTransform = null;

    execute(new IExecutor() {
        Transform returnTransform = null;
        @Override
        public boolean execute(Transform transform) {
            if (transform.getID() == id) {
                returnTransform = transform;
                return true;
            }
            return false;
        }
    });

    return <returnTransform>;
}

What I need, is get the variable 'returnTransform' from inside the interface and use it to return from the 'findById' method. 我需要的是从接口内部获取变量“ returnTransform”,并使用它从“ findById”方法中返回。
I can't define a variable in the method, because it has to be final to access within the method: and I need to change it. 我无法在方法中定义变量,因为在方法内访问它必须是最终变量:我需要对其进行更改。

One solution would be to define the 'returnTransform' variable directly in the class, but it seems tacky. 一种解决方案是直接在类中定义“ returnTransform”变量,但这似乎很俗气。 Any other genius ideas from anyone? 还有其他天才的想法吗?
Thanks :) 谢谢 :)

You may define your own interface, which would have method to return returnTransform , something like: 您可以定义自己的接口,该接口将具有返回returnTransform方法,例如:

public interface ExecutorWithTransform extends IExecutor {
    public Transform getTransform();
}

Then you should use this interface in code, when creating instance: 然后,在创建实例时,应在代码中使用此接口:

ExecutorWithTransform executor = new ExecutorWithTransform() {

    Transform returnTransform = null;

    public Transform getTransform() {
        return returnTransform;
    }

    @Override
    public boolean execute(Transform transform) {
        if (transform.getID() == id) {
            returnTransform = transform;
            return true;
        }
        return false;
    }
};

execute(executor);

return executor.getTransform();

Also, there's a workaround, which will allow you using non-final variables inside the anonymous interface. 另外,有一种解决方法,可以让您在匿名界面中使用非最终变量。 You should just wrap your varible with some variable holder, for example, AtomicReference : 您应该只用一些变量持有者包装变量,例如AtomicReference

final AtomicReference<Transform> returnTransform = new AtomicReference<>();

execute(new IExecutor() {
    @Override
    public boolean execute(Transform transform) {
        if (transform.getID() == id) {
            returnTransform.set(transform);
            return true;
        }
        return false;
    }
});

return returnTransform.get();

But this is not supposed to be idiomatic way. 但这不应该是惯用的方式。

You can create a local class, that wraps the Transform variable and exposes accessor methods for it. 您可以创建一个本地类,该类包装Transform变量并为其提供访问器方法。 Then you can use a final wrapper instance and you will be allowed to change its internal state. 然后,您可以使用final包装实例,并且可以更改其内部状态。

For example: 例如:

public Transform findById(final int id) {
    class Wrapper {
        private Transform returnTransform = null;

        Transform getReturnTransorm() { return returnTransform; }

        void setReturnTransform(Transform returnTransform) {
            this.returnTransform = returnTransform;
        }
    }
    final Wrapper wrapper = new Wrapper();
    execute(new IExecutor() {
        @Override
        public boolean execute(Transform transform) {
            if (transform.getID() == id) {
                wrapper.setReturnTransform(transform);
                return true;
            }
            return false;
        }
    });

    return wrapper.getReturnTransform();
}

创建一个类实现IExecutor其中包含类型的字段Transform与getter和其中规定它execute -那么你可以通过这个类的一个对象来executegetTransform之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM