aps*_*nce 4 interface actionscript-3
从我读到的,接口基本上是方法的类,对吧?如果两个类实现相同的接口,那么它们都应该具有接口中描述的方法.
现在,这有用吗?假设我想调用foo();.
public interface IExample {
function foo(om:String):void;
}
class HungryClass implements IExample{
public function foo(om:String):void{
trace("OM NOM NOM!!! Thank you for feeding me" + om);
}
}
class FullClass implements IExample{
public function foo(om:String):void{
trace("No thanks, I don't want to eat" + om);
}
}
//somewhere..
instanceOfEitherClass.foo("cake");
Run Code Online (Sandbox Code Playgroud)
接口如何帮助?如果没有接口,这不会有用吗?
谢谢
假设您有一个继承自抽象类的具体类.在这种情况下,您只需执行以下操作:
public class ConcreteClass extends AbstractClass
Run Code Online (Sandbox Code Playgroud)
现在,如果你需要具体的类来继承这个EventDispatcher类呢?您不能执行以下操作:
public class ConcreteClass extends AbstractClass, EventDispatcher
Run Code Online (Sandbox Code Playgroud)
但是,您可以实现EventDispatcher类的IEventDispatcher接口,然后使用如下所示的EventDispatcher对象:
internal class ConcreteClass extends AbstractClass implements IEventDispatcher
{
private var _eventDispatcher:EventDispatcher;
public function ConcreteClass()
{
_eventDispatcher = new EventDispatcher(this);
}// end function
public function addEventListener(type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false):void
{
_eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}// end function
public function dispatchEvent(event:Event):Boolean
{
return _eventDispatcher.dispatchEvent(event);
}// end function
public function hasEventListener(type:String):Boolean
{
return _eventDispatcher.hasEventListener(type);
}// end function
public function removeEventListener(type:String,
listener:Function,
useCapture:Boolean = false):void
{
_eventDispatcher.removeEventListener(type, listener, useCapture);
}// end function
public function willTrigger(type:String):Boolean
{
return _eventDispatcher.willTrigger(type);
}// end function
}// end class
Run Code Online (Sandbox Code Playgroud)
使用组合和接口的这种组合,您可以将具体类用作an AbstractClass和EventDispatcherobject.
接口非常适合允许"不相关的对象彼此通信".您可以在此处找到有关接口的更多信息.
| 归档时间: |
|
| 查看次数: |
2373 次 |
| 最近记录: |