向下转换参考对象时,是什么导致 MOVE_CAST_ERROR ?

发布于 2024-08-08 20:28:20 字数 512 浏览 11 评论 0原文

我正在创建一个从具有受保护实例化的父类继承的类。超类有一个提供该类实例的静态方法。

以下代码会导致运行时错误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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2024-08-15 20:28:20

我没有在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文