本文介绍了instanceof检查有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着泛型的引入,我不愿意尽可能地执行instanceof或者casting。但在这种情况下我没有看到解决方法:

With the introduction of generics, I am reluctant to perform instanceof or casting as much as possible. But I don't see a way around it in this scenario:

for (CacheableObject<ICacheable> cacheableObject : cacheableObjects) {
    ICacheable iCacheable = cacheableObject.getObject();
    if (iCacheable instanceof MyObject) {
        MyObject myObject = (MyObject) iCacheable;
        myObjects.put(myObject.getKey(), myObject);
    } else if (iCacheable instanceof OtherObject) {
        OtherObject otherObject = (OtherObject) iCacheable;
        otherObjects.put(otherObject.getKey(), otherObject);
    }
}

在上面的代码中,我知道我的ICacheables应该只有MyObject或OtherObject的实例,并且根据这个我想把它们放到2个单独的地图中然后再进行一些处理。

In the above code, I know that my ICacheables should only ever be instances of MyObject, or OtherObject, and depending on this I want to put them into 2 separate maps and then perform some processing further down.

我会是感兴趣,如果有其他方法没有我的instanceof检查。

I'd be interested if there is another way to do this without my instanceof check.

谢谢

推荐答案

您可以使用双重调用。没有承诺它是一个更好的解决方案,但它是另一种选择。

You could use double invocation. No promises it's a better solution, but it's an alternative.

import java.util.HashMap;

public class Example {

    public static void main(String[] argv) {
        Example ex = new Example();
        ICacheable[] cacheableObjects = new ICacheable[]{new MyObject(), new OtherObject()};

        for (ICacheable iCacheable : cacheableObjects) {
            // depending on whether the object is a MyObject or an OtherObject,
            // the .put(Example) method will double dispatch to either
            // the put(MyObject) or  put(OtherObject) method, below
            iCacheable.put(ex);
        }

        System.out.println("myObjects: "+ex.myObjects.size());
        System.out.println("otherObjects: "+ex.otherObjects.size());
    }

    private HashMap<String, MyObject> myObjects = new HashMap<String, MyObject>();
    private HashMap<String, OtherObject> otherObjects = new HashMap<String, OtherObject>();

    public Example() {

    }

    public void put(MyObject myObject) {
        myObjects.put(myObject.getKey(), myObject);
    }

    public void put(OtherObject otherObject) {
        otherObjects.put(otherObject.getKey(), otherObject);
    }

}

interface ICacheable {
    public String getKey();
    public void put(Example ex);
}

class MyObject implements ICacheable {

    public String getKey() {
        return "MyObject"+this.hashCode();
    }

    public void put(Example ex) {
        ex.put(this);
    }
}

class OtherObject implements ICacheable {

    public String getKey() {
       return "OtherObject"+this.hashCode();
    }

    public void put(Example ex) {
        ex.put(this);
    }

}

这里的想法是 - 而不是铸造或使用 instanceof - 你调用 iCacheable 对象的 .put(...) 将自身传递回示例对象的重载方法的方法。调用哪种方法取决于该对象的类型。

The idea here is that - instead of casting or using instanceof - you call the iCacheable object's .put(...) method which passes itself back to the Example object's overloaded methods. Which method is called depends on the type of that object.

另请参阅。我的代码示例闻起来,因为 ICacheable.put(...)方法是不完整的 - 但使用访问者模式中定义的接口可以清除这种气味。

See also the Visitor pattern. My code example smells because the ICacheable.put(...) method is incohesive - but using the interfaces defined in the Visitor pattern can clean up that smell.

在Java中,覆盖总是在运行时绑定,但重载稍微复杂一点:动态调度意味着将选择方法的实现在运行时,但方法的签名仍然在编译时确定。 (查看了解更多信息,还可以查看本书第137页上的益智游戏制作哈希#href=\"http://www.javapuzzlers.com/\" rel=\"nofollow\"> Java Puzzlers 。)

In Java, overriding is always bound at runtime, but overloading is a little more complicated: dynamic dispatching means that the implementation of a method will be chosen at runtime, but the method's signature is nonetheless determined at compile time. (Check out the Java Language Specification, Chapter 8.4.9 for more info, and also check out the puzzler "Making a Hash of It" on page 137 of the book Java Puzzlers.)

这篇关于instanceof检查有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:00