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 Ev开发者_如何学编程entDispatcher 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]
?
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) ...
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, using some metadata, and the flash.events
package, looking very nice in MXML, but being an impressive number of calls and instantiations in 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));
... 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 ...
hope that helped ...
You can define [Event]
metadata in interfaces for informational purposes, but unlike functions, you cannot enforce that implementations use those events. In my experience, you must re-define the events in the implementation classes.
精彩评论