考虑的instanceof有害吗? [英] instanceof considered harmful?
问题描述
我找了一些code有大量使用Java的的的instanceof
运营商。我会争取的诱惑,进入细节,因为这是一个普遍的问题。 的instanceof
让我想起了C ++的的动态转换,和我读到关于它的建议,不久以前,如果你认为你需要它,可能是你的模式是关闭的。这仍然是传统的智慧?它是否适用于Java的的instanceof
?
I am looking over some code that has heavy use of Java's instanceof
operator. I will fight the temptation to go into details, since this is a general question. instanceof
reminds me of of c++'s dynamic cast, and the advice I read about it, long ago, that if you think you need it, probably your model is off. Is this still the conventional wisdom? Does it apply to Java's instanceof
?
推荐答案
是的,你是在正确的轨道上。而的instanceof
肯定有它的用途,大量使用通常表明您一流的设计是有缺陷的。
Yes, you are on the right track. While instanceof
certainly has its uses, heavy use generally indicates that your class design is deficient.
这篇关于考虑的instanceof有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!