简体   繁体   English

Flex / AS3元数据标签在接口定义中的处理方式是否与类定义不同?

[英]Are Flex / AS3 metadata tags handled differently at interface definitions than at class definitions?

If I use metadata tags in front of an interface, is that the same as in front of a class? 如果我在界面前使用元数据标签,那是否与类前面的相同? I. e., can I do 我也可以这样做

[Event(name="fooUpdate", type="com.acme.events.FooEvent")
public interface IFoozer extends IEventDispatcher
{
}

// ... now omit that metadata in the implementations ...

public class Foozer extends EventDispatcher implements IFoozer
{
    public function set bar(b:Bar):void
    {
        this.dispatchEvent(new FooEvent(FooEvent.FOO_UPDATE));
    }
}

// ... and expect it to be known in MXML ...

<acme:Foozer fooUpdate="myUpdateHandler">
  <!-- ... -->
</acme:Foozer>

Or, similarly, with [Bindable] ? 或者,类似地, [Bindable]

yes and no ... for [Event] yes ... but [Bindable] is not just a metadata tag ... [Bindable] (without arguments!!) instructs the compiler to generate AS3 code, that will make sure a PropertyChangeEvent is dispatched if you set the property (you can see that if you let the mxmlc keep generated AS3 code) ... 是和否...对于[Event] yes ...但是[Bindable]不仅仅是元数据标签... [Bindable] (没有参数!!)指示编译器生成AS3代码,这将确保PropertyChangeEvent如果设置属性,则调度(如果让mxmlc保持生成AS3代码,则可以看到)...

also, for [Embed] this is not the case ( [Embed] must be followed by a variable. interfaces cannot have variables) ... you will always be able to retrieve the metadata through flash.utils::describeType ... this is quite inconsistent, since some metadata tags really provide just metadata compiled into the output, and some metadata instructs the compiler to really take action ... yet again, metadata provided by some tags is so heavily used by the flex framework, they become a language feature in MXML (as [Event] ) ... but they are not in AS3 ... however AS3 is a pretty close represantation of what happens on the VM, whereas MXML and Flex Framework both are only vaguely related to it ... this is also why ActionScript classes and MXML components interoperate very horribly (you might have noticed MXML doesn't have the concept of interfaces, execution flow (and thus time), whereas AS3 doesn't have the concepts of events or bindings (which are "native" in MXML, but are built on top of AS3, us 另外,对于[Embed]情况并非如此( [Embed]必须后跟一个变量。接口不能有变量)...你总能通过flash.utils::describeType检索元数据...这个是非常不一致的,因为一些元数据标签实际上只提供编译到输出中的元数据,并且一些元数据指示编译器真正采取行动......同样,一些标签提供的元数据被flex框架大量使用,它们变成了MXML中的语言功能(如[Event] )...但它们不在AS3中......但是AS3对VM上发生的事情非常接近,而MXML和Flex Framework都只是模糊地关联它。这也是ActionScript类和MXML组件非常可怕地互操作的原因(你可能已经注意到MXML没有接口,执行流程(以及时间)的概念,而AS3没有事件或绑定的概念(其中)在MXML中是“原生的”,但它们建立在我们的AS3之上 ing some metadata, and the flash.events package, looking very nice in MXML, but being an impressive number of calls and instantiations in AS3)) ... 一些元数据,以及flash.events包,在MXML中看起来非常好,但在AS3中是一个令人印象深刻的调用和实例化))...

what you want, probably will not work ... i personally think, it's more of a flaw that interfaces are allowed to have metadata at all ... nearly all metadata is for use at runtime ... at runtime, every object has its own class and interfaces are secondary for introspection/reflection ... 你想要什么,可能不会工作...我个人认为,更多的是允许接口拥有元数据的缺陷...几乎所有元数据都是在运行时使用...在运行时,每个对象都有它的自己的类和接口是次要的内省/反思......

so back to the old school phrase: an interface requires behaviour, a class provides implementation ... [Event(name="fooUpdate", type="com.acme.events.FooEvent")] is pure metadata ... and metadata is implementation in the world of AS3, because this does not require anyone to do anything (you might as well just write [Bar(foo="123")] ) ... it is a tag to put on top of a class, if and only if the class has this very line somewhere within its implementation: this.dispatchEvent(new FooEvent(FooEvent.FOO_UPDATE)); 所以回到旧学校的短语: 一个接口需要行为,一个类提供实现 ... [Event(name="fooUpdate", type="com.acme.events.FooEvent")]是纯元数据......和元数据是在AS3的世界中实现,因为这不需要任何人做任何事情(你可能只是写[Bar(foo="123")] )...它是一个放在一个类之上的标记,当且仅当该类在其实现中的某处具有此行时: this.dispatchEvent(new FooEvent(FooEvent.FOO_UPDATE)); ... what you are kind of trying to say is, any IFoozer implementation will dispatch FooEvent.FOO_UPDATE ... this is a guarantee the compiler cannot provide, because it doesn't check metadata vs. implementation ... ...你有点想说的是,任何IFoozer实现都会调度FooEvent.FOO_UPDATE ......这是编译器无法提供的保证,因为它不检查元数据与实现...

hope that helped ... 希望有帮助......

You can define [Event] metadata in interfaces for informational purposes, but unlike functions, you cannot enforce that implementations use those events. 您可以在接口中定义[Event]元数据以供参考,但与功能不同,您无法强制实现使用这些事件。 In my experience, you must re-define the events in the implementation classes. 根据我的经验,您必须在实现类中重新定义事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM