如果是父实例,instanceof 是否返回 true?
我有一个扩展Parent
的Child
类。
Parent child = new Child();
if (child instanceof Parent){
// Do something
}
这返回 true 还是 false,为什么?
I have a class Child
that extends Parent
.
Parent child = new Child();
if (child instanceof Parent){
// Do something
}
Does this return true or false, and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,会的。为什么不应该呢?
因为child实际上是Parent的一个实例。如果您只想对子项执行操作,您应该检查
但是您应该记住 Scott Meyers 的《Effective C++》中的以下语句:
我认为这也适用于这种情况。如果您想根据引用对象所属的类类型做一些事情,以下代码结构应该可以帮助您。
<强>注意:我还没有编译它
编辑:一个更好的例子
您可以开发一个绘图应用程序,可以绘制任何类型的形状。在这种情况下,您应该有一个abstract 类型
Shape
出于以下目的:绘制所有形状;查找形状或删除形状,您需要有一个列表。 的 Shapes。由于该列表是父类型,因此它可以存储任何形状。
Shape 接口/抽象类/虚拟类 应该有一个。 >抽象/纯虚函数
Draw()
。因此,在 DrawToDeviceLoop 中,您只需为每个形状调用Draw()
,而无需检查它的Shape< 。 /code> 接口可以有一个抽象实现
AbstractShape
,它可以将形状名称或id作为数据成员以及GetName、Cleanup和其他具有所有形状通用功能的函数。请记住,抽象类型无法被实例化,因此
Shape
本身也无法被实例化,因为它也无法被绘制。编辑2:多态性和异常处理 - user1955934询问“关于检查异常类怎么样”对于异常处理关于多态性的最佳实践是:
因此,其原则上是相同的,如果需要以不同方式处理异常,则应定义子/特定类,并且应捕获特定异常(不检查instanceof)
以了解更多最佳实践异常处理。请参阅 9 个 Java 中处理异常的最佳实践和异常的最佳实践 (C#)
编辑3:我承认这条规则有一个例外
因此,我正在使用遗留代码(用 C++ 编写),该代码大部分是在大约 15 年前编写的,它们总是检查子类是否执行某些操作行动。我被要求添加一些具有相同逻辑的代码,我告诉我的经理(他也是开发人员)我不能这样做,指向这个答案,然后我们讨论并接受了这个规则的例外。在我们的例子中,这个父类自 2000 年以来只有 2 个子类,并且我们看不到在不久的将来添加任何子类,由于核心代码的增长受到限制,我们决定不添加子类和当前的子类。 number 只是 2,检查类型会更有效,尤其是当代码一直这样编写时。
不过,这种检查的实例并不多,父级主要实现复杂的功能,并且它更多地用于共享代码,然后专门/区分它们。
Yes, it would. And why should it not?
Because child is in fact an instance of Parent. If, you want to perform an operation only for a child you should check
However you should remember the following statement from Effective C++, by Scott Meyers :
which I think applies in this case too. If you want to doSomething based on what type of class the referenced object belongs to, the following code structure should help you with it.
NOTE: I have not compiled it.
EDIT: A better example
You could be developing a drawing application. An application that draws shapes of any kind. In that case, you should have an abstract type
Shape
.For purpose(s) like; drawing all shapes; list all shapes; find a shape or delete a shape, you need to have a list of Shapes. Since the list is of a parent type, it can store any shapes.
The
Shape
interface/abstract class/virtual class should have an abstract/pure virtual functionDraw()
. So, in your DrawToDeviceLoop, you just callDraw()
for each shape, you never need to check what shape it is.The
Shape
interface can have an abstract implementationAbstractShape
, which can have shape name or id as data members and GetName, Cleanup and other functions with functionality common to all shapes.Remember an abstract type cannot be instantiated, so
Shape
itself cannot be instantiated, as it cannot be drawn either.EDIT 2: Polymorphism and Exception Handling - user1955934 asked "What about checking for exception class" For exception handling the best practices with respect to polymorphism are:
So, its principally the same, if an exception needs to be handled differently, a child/specific class should be defined, and the specific exception should be caught (not checked instanceof)
To know more best practices on exception handling. See 9 Best practices to handle exception in Java and Best practices for exceptions (C#)
EDIT 3: I admit to an exception in this rule
So, I am working with a legacy code (written in C++), which has mostly been written about 15 years ago, where they always check for the child class to perform certain actions. I was asked to add some code with the same logic, and I told my manager (he is a Dev too) I cannot do this, pointing to this answer, and then we discussed and accepted to this exception to the rule. In our case, this parent class has had just 2 children since the year 2000, and we do not see any child being added in the near future, with the core code limited for growth, we decided that with no addition to child classes and current number being just 2, it is more efficient to check for type, especially when that is how the code has been written, since ever.
There aren't many instances of this check too though, the parent implements the complicated functionalities mostly and it exists more for sharing code then specializing/differentiating them.
instanceof 如果它是子类,将返回 true...
文档实例
instanceof will return true if it's a subclass...
instanceof Documentation
当然它返回true,因为孩子是父母的一个实例
ofcourse it returns true because child is an instance of the parent
是。每当引用(
instanceof
表达式的左侧)可以转换为ReferenceType(instanceof
表达式右侧的类型)时,instanceof
将为 trueinstanceof
表达式)。对于与其父类相关的子类来说也是如此:来自 Java 语言规范,Java SE 9 版:
Yes.
instanceof
will be true whenever the reference(left side of theinstanceof
expression) can be cast to the ReferenceType(the type on the right side of theinstanceof
expression). This will be true for subclasses in relation to their parent:From The Java Language Specification, Java SE 9 Edition: