Actionscript 重写扩展接口与 Java 中的方法?

发布于 2024-12-11 01:48:45 字数 783 浏览 0 评论 0原文

与 Actionscript 语义相比,我对 Java 的类和接口语义更加熟悉,但我有一个在 Java 中工作但在 Actionscript 中不起作用的代码示例。这种衰退是一个严重的问题,因为我试图通过 Java DTO 编码生成我的 Actionscript 值对象,除非语义相同,否则我会遇到很大的麻烦。

以下是在 Java 中工作但无法在 Actionscript 中编译的代码:

接口 A:

public interface Ia {
    function makeCopy():Ia;
}

接口 B:

public interface Ib extends Ia {
}

类 B (不会编译):

public class B implements Ib {
    public function makeCopy():Ib {
        return null;
    }
}

我不明白为什么类 B 会抛出有关“makeCopy”不兼容签名的编译错误显然,接口 B 扩展了接口 A...因此不存在类型冲突或不兼容性。如果这只是 Actionscript 的限制,任何人都可以建议一种重新编码的方法吗?

注意:我已经尝试将接口 B 更改为此,并且它在接口 B 中引发了错误(在 Java 中工作):

public interface Ib extends Ia {
    function makeCopy():Ib;
}

I am much more familiar with Java's semantics of class and interface than with Actionscript semantics, but I have an example of some code that works in Java and doesn't work in Actionscript. This descrepency is a serious problem in that I am trying to code generate my Actionscript value objects from the Java DTOs and unless the semantics are the same, I am in deep trouble.

Here's the code that works in Java and fails to compile in Actionscript:

Interface A:

public interface Ia {
    function makeCopy():Ia;
}

Interface B:

public interface Ib extends Ia {
}

Class B (won't compile):

public class B implements Ib {
    public function makeCopy():Ib {
        return null;
    }
}

I don't understand why class B throws a compile error about an incompatible signature for "makeCopy" when clearly interface B extends interface A...thus there is no violation of type or incompatibility. If this is just an Actionscript limitation, can anyone suggest a way to recode?

NOTE: I already tried changing interface B to this and it threw an error in interface B (which work in Java):

public interface Ib extends Ia {
    function makeCopy():Ib;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梅窗月明清似水 2024-12-18 01:48:45

在 AS3 中,方法签名在实现和继承中必须相同,因此如果您最初将 makeCopy 的返回类型定义为 Ia,那么它必须保留在所有后代接口中及其实现。

但是,您可以做的是通过使用 Ia 签名的函数返回实现 Ib 的对象实例,因为它仍然是 Ia 的有效实现:

public class B implements Ib {

    public function makeCopy():Ia {
        return this;
    }

    public function B() {
        trace(makeCopy() is Ia);  //true
        trace(makeCopy() is Ib);  //true
        trace(makeCopy() is B);   //true
    }
}

In AS3, method signatures must be identical in both implementation and inheritance, so if you define the return type of makeCopy as Ia initially, that's how it must stay in all descendant interfaces and implementations thereof.

What you can do however, is return an instance of an object that implements Ib through a function signed with Ia, because it will still be a valid implementation of Ia:

public class B implements Ib {

    public function makeCopy():Ia {
        return this;
    }

    public function B() {
        trace(makeCopy() is Ia);  //true
        trace(makeCopy() is Ib);  //true
        trace(makeCopy() is B);   //true
    }
}
你曾走过我的故事 2024-12-18 01:48:45

从 ActionScript 的上下文来看,makeCopy() 的返回类型具有不兼容的签名。

接口 Ia 定义 makeCopy 返回 Ia。

接口 Ib 扩展将从 makeCopy 返回 Ia 基数。将 makecopy():Ib 添加到接口 Ib 是对 Ia 中定义的不兼容重写。

在 B 类中,不兼容的签名期望 makeCopy 返回 Ia。

也许您想要完成的更像是一个抽象类,您应该在其中扩展 A 类和 B 类。

From the context of ActionScript, the return type of makeCopy() has an incompatible signature.

Interface Ia defines makeCopy returning Ia.

Interface Ib extension would return Ia base from makeCopy. Adding makecopy():Ib to interface Ib is an incompatible override to the definition in Ia.

In class B, the incompatible signature expects makeCopy to return Ia.

Perhaps what you're trying to accomplish is more like an Abstract Class, where you should extend A and B classes.

埋葬我深情 2024-12-18 01:48:45

接口上的“makeCopy()”返回一个Ia。而在实现中它返回一个 Ib。除非 Ib 是 Ia,否则就会失败。

"makeCopy()" on the interface returns an Ia. Whereas in the implementation it returns an Ib. Unless an Ib is an Ia it is going to fail.

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