ActionScript의 인라인 인터페이스 구현
-
16-09-2019 - |
문제
ActionScript에서 이와 같은 것이 가능합니까?
자바:
URLFetcherFactory.setCreator(
new IURLFetcherCreator() {
public IURLFetcher create() {
return new URLFetcher();
}
}
);
ActionScript :
?
나는 이것에 대해 궁금해하고 가능하다는 것을 나타내는 것을 찾을 수 없었습니다. 그것을 알아 냈다 ~였다 가능하면 여기에서 답을 찾을 수 있습니다. 감사! Stackoverflow 바위!
해결책
인터페이스 인스턴스를 만들 수 없습니다. 그러나 공장 수업을 만들 수 있습니다.
public class URLFetcherCreator : IURLFetcherCreator {
private var _cls : Class;
public URLFetcherCreator(Class cls) {
this._cls = cls;
}
public function create() : IURLFetcher
{
return new cls();
}
}
또는 iurlfetcher를 반환하는 함수를 수락하도록 setReator를 변경하십시오.
URLFetcherFactory.setCreator(
function() : IURLFetcher {
return new URLFetcher();
}
);
다른 팁
이 시도:
URLFetcherFactory.setCreator(
new IURLFetcherCreator() {
public function create():IURLFetcher {
return new URLFetcher();
}
}
);
AS3에서 익명의 내부 클래스를 사용할 수 없습니다. 콜백과 같은 특별한 경우 익명 내부 클래스 대신 기능을 사용할 수 있습니다.
자바:
interface Callback {
void done(String info);
}
class Service {
void process(Callback callback);
}
...
myService.process(new Callback() {
void done(String info) {
// trace(info);
}
}
AS3 :
class Service {
public function process(callback:Function):void;
}
...
myService.process(function(info:String):void {
trace(info);
});
제휴하지 않습니다 StackOverflow