如果父项的实例,instanceof是否返回true? [英] Does instanceof return true if instance of a parent?

查看:176
本文介绍了如果父项的实例,instanceof是否返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Child ,它扩展了 Parent

Parent child = new Child();

if (child instanceof Parent){
    // Do something
}

这是返回true还是false,为什么?

Does this returns true or false, and why?

推荐答案

,它会。为什么不呢?

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

if (child instanceof Child){
}

但是你应该记住Scott Meyers的以下有效C ++声明:

However you should remember the following statement from Effective C++, by Scott Meyers :


任何时候你发现自己编写表格的
代码如果对象是
类型的T1,那么做一些事情,但如果
是T2的类型,然后做一些
其他的事情,打自己。

"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.

我认为适用于这种情况也是如此。如果你想基于引用对象所属的类类型 doSomething ,下面的代码结构可以帮助你。

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.

注意:我还没有编译它。

class Parent {
    public void doSomething() {
        System.out.println("I am the Parent, and I do as I like");
    }
}

class ChildA extends Parent {
    public void doSomething() {
        System.out.println("I am a child named A, but I have my own ways, different from Parent");
    }
}

class ChildB extends Parent {
    public void doSomething() {
        System.out.println("I am a child named B, but I have my own ways, different from my Parent and my siblings");
    }
}

public class Polymorphism101 {

    public static void main() {

        Parent p = new Parent();
        p.doSomething();

        p = new ChildA();
        p.doSomething();

        p = new ChildB();
        p.doSomething();

    }

}

编辑:一个更好的例子

你可能正在开发一个绘图应用程序。绘制任何形状的应用程序。在这种情况下,您应该有 abstract 类型 Shape

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.

Shape 接口/抽象类/虚拟类应该有一个抽象/纯虚拟函数 Draw()。所以,在你的DrawToDeviceLoop中,你只需为每个形状调用 Draw(),你永远不需要检查它是什么形状。

The Shape interface/abstract class/virtual class should have an abstract/pure virtual function Draw(). So, in your DrawToDeviceLoop, you just call Draw() for each shape, you never need to check what shape it is.

Shape 界面可以有一个抽象实现 AbstractShape ,它可以有形状名称或id作为数据成员和GetName,Cleanup以及具有所有形状共有功能的其他函数。

The Shape interface can have an abstract implementation AbstractShape, which can have shape name or id as data members and GetName, Cleanup and other functions with functionality common to all shapes.

记住抽象类型不能实例化,所以 Shape 本身无法实例化,因为它也无法绘制。

Remember an abstract type cannot be instantiated, so Shape itself cannot be instantiated, as it cannot be drawn either.

这篇关于如果父项的实例,instanceof是否返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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