I have gone through various articles ,but i still do not know why instanceof should not used.kindlylet me know your thoughts.
我已经浏览了很多文章,但我仍然不知道为什么不应该使用instanceof。请让我知道你的想法。
5 个解决方案
#1
14
I find a need to use instanceof hints at bad design. It's a sure sign that a big, complex switch-style construct will follow. Most other times I see it used, we should use polymorphism rather than instanceof. See the Strategy pattern. (relevant examples of use)
我发现有必要使用一些暗示糟糕设计的实例。这是一个明显的标志,一个巨大的、复杂的开关样式的构造将随之而来。大多数时候我看到它被使用,我们应该使用多态性而不是instanceof。策略模式。(相关的例子使用)
The only time I find I need to use it is when implementing equals(Object o)
.
我唯一需要使用它的时间是在实现equals(Object o)时。
#2
4
Casting from a base type to a derived type is a bad thing. If you use instandof that way, it's considered bad design because hard to maintain and read. See http://www.javapractices.com/topic/TopicAction.do?Id=31.
将基类型转换为派生类型是一件坏事。如果你使用这种方式,它被认为是糟糕的设计,因为很难维护和阅读。见http://www.javapractices.com/topic/TopicAction.do?Id=31。
Using instanceof
for equals() because you have an object
that should be your type, that's good practice.
使用instanceof for equals()因为您有一个对象,该对象应该是您的类型,这是很好的实践。
#3
3
One good use-case would be checking for marker interfaces like RandomAccess.
一个很好的用例是检查诸如RandomAccess之类的标记接口。
#4
0
When you know the object being passed then you need not use it. If there is any ambiguity involved (like two classes implementing the same interface) then it is advised to use instanceof
before proceeding further.
当您知道要传递的对象时,就不需要使用它。如果涉及任何歧义(如两个实现相同接口的类),则建议在进一步进行之前使用instanceof。
#5
0
Using instanceof when overriding Object's equals method is necessary, seeing as two objects cannot be equal if they are of different types. I've rarely come accross another situation where instanceof is required - perhaps if downcasting is required but a ClassCastException may be thrown.
当需要重写对象的equals方法时,使用instanceof,如果两个对象具有不同的类型,则不能视为相等。我很少遇到另一个需要instanceof的情况——如果需要降级,但可能抛出ClassCastException。
#1
14
I find a need to use instanceof hints at bad design. It's a sure sign that a big, complex switch-style construct will follow. Most other times I see it used, we should use polymorphism rather than instanceof. See the Strategy pattern. (relevant examples of use)
我发现有必要使用一些暗示糟糕设计的实例。这是一个明显的标志,一个巨大的、复杂的开关样式的构造将随之而来。大多数时候我看到它被使用,我们应该使用多态性而不是instanceof。策略模式。(相关的例子使用)
The only time I find I need to use it is when implementing equals(Object o)
.
我唯一需要使用它的时间是在实现equals(Object o)时。
#2
4
Casting from a base type to a derived type is a bad thing. If you use instandof that way, it's considered bad design because hard to maintain and read. See http://www.javapractices.com/topic/TopicAction.do?Id=31.
将基类型转换为派生类型是一件坏事。如果你使用这种方式,它被认为是糟糕的设计,因为很难维护和阅读。见http://www.javapractices.com/topic/TopicAction.do?Id=31。
Using instanceof
for equals() because you have an object
that should be your type, that's good practice.
使用instanceof for equals()因为您有一个对象,该对象应该是您的类型,这是很好的实践。
#3
3
One good use-case would be checking for marker interfaces like RandomAccess.
一个很好的用例是检查诸如RandomAccess之类的标记接口。
#4
0
When you know the object being passed then you need not use it. If there is any ambiguity involved (like two classes implementing the same interface) then it is advised to use instanceof
before proceeding further.
当您知道要传递的对象时,就不需要使用它。如果涉及任何歧义(如两个实现相同接口的类),则建议在进一步进行之前使用instanceof。
#5
0
Using instanceof when overriding Object's equals method is necessary, seeing as two objects cannot be equal if they are of different types. I've rarely come accross another situation where instanceof is required - perhaps if downcasting is required but a ClassCastException may be thrown.
当需要重写对象的equals方法时,使用instanceof,如果两个对象具有不同的类型,则不能视为相等。我很少遇到另一个需要instanceof的情况——如果需要降级,但可能抛出ClassCastException。