考虑的instanceof有害吗? [英] instanceof considered harmful?

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

问题描述

我找了一些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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆