[英]Why in OO programming for casting objects instantiate super class and subclass in a variable?
For eg: ParentObj A = new ChildObj(); ((ChildObj) A).childMethod();
例如:
ParentObj A = new ChildObj(); ((ChildObj) A).childMethod();
ParentObj A = new ChildObj(); ((ChildObj) A).childMethod();
I see lots of instances like this in object oriented programming, would it be easier to just declare the ChildObj as ChildObj A = new ChildObj()
instead of using the parent object as the starter of the of the creation of the object? 我在面向对象的编程中看到了很多这样的实例,将ChildObj声明为
ChildObj A = new ChildObj()
而不是使用父对象作为创建对象的开始者会更容易吗?
What would be the use cases of this example to any object oriented programming? 对于任何面向对象的编程,该示例的用例是什么?
And will the A object be the ChildObj in this declaration or ParentObj? 并且A对象是该声明中的ChildObj还是ParentObj?
Is there like a performance boost and such as to doing it this way or its a use case base thing if there is any? 有没有像这样的性能提升和诸如此类的提高性能(如果有的话)?
thanks noob here. 谢谢新手在这里。
Its my personal opinion and mostly its true that there is no use case of such declaration, like 我个人的看法,并且基本上是这样的,即没有此类声明的用例,例如
ParentObj A = new ChildObj();
ParentObj A =新的ChildObj();
We use to hide the child object under Parent references in below cases 在以下情况下,我们用于在“父对象”引用下隐藏子对象
These are some of use cases. 这些是一些用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.