向下转换参考对象时,是什么导致 MOVE_CAST_ERROR ?
我正在创建一个从具有受保护实例化的父类继承的类。超类有一个提供该类实例的静态方法。
以下代码会导致运行时错误MOVE_CAST_ERROR
:
data: o_child type ref to zchild.
o_child ?= zparent=>provide_instance( ).
我也尝试过:
data: o_parent type ref to zparent,
o_child type ref to zchild.
o_parent = zparent=>provide_instance( ).
o_child ?= o_parent.
我过去曾成功地向下转换其他对象类型 - 有谁知道在 ZPARENT 或 ZCHILD 中寻找什么这可能会使两个类不兼容?
目前 ZCHILD 只有一种额外的方法,如果将其添加到超类中,就会破坏该类的抽象,所以我不太愿意这样做。
I'm creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class.
The following code causes a run-time error MOVE_CAST_ERROR
:
data: o_child type ref to zchild.
o_child ?= zparent=>provide_instance( ).
I have also tried:
data: o_parent type ref to zparent,
o_child type ref to zchild.
o_parent = zparent=>provide_instance( ).
o_child ?= o_parent.
I have succesfully down-casted with other object types in the past - does anyone have an idea what to look for in ZPARENT or ZCHILD that may make the two classes incompatible?
Currently ZCHILD only has one extra method, which if added to the super class would break the abstraction of the class, so I'm not that keen to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有在abap中进行任何面向对象的编码。也没有太多的abap编程。但我认为这是一个典型的oo场景。这是我的猜测。
你不能将一个对象转换成它不该有的样子。
您已经创建了父类的实例。现在子类具有父类没有的“myCandy”属性。所以你的对象没有这个属性。然后你把它投射给孩子。当您请求(或更改)“myCandy”属性时,糟糕的运行时间会做什么?它什么也做不了。所以它不允许这个演员。
仅当该对象被实例化为子对象,然后将其转换为父对象,然后再次返回子对象时,才可以进行转换。孩子拥有父母拥有的一切,所以这条路没有问题。
I didn't do any object oriented coding in abap. Nor much abap programming. But I think that this is a typical oo scenario. So here is my guess.
You can't cast an object to what it ain't.
You have created an instance of the parent class. Now the child class has the property of "myCandy" that the parent class don't have. So your object don't have this property. And then you cast it to child. What the poor run time has to do when you ask for ( or change ) the "myCandy" property? It can do nothing. So it disallow this cast.
The cast is only possible if the object was instantiated as the child and then it was casted to the parent object and then back again to the child object. The child has everything the parent has so there is no problem with this path.