问题描述
我正在查看一些大量使用 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.
这篇关于被认为有害的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!