我想实现这个方法
函数isInstance(一:类,B:类):布尔;
这是AS3与类的工作。 注意,影片剪辑扩展Sprite。的
跟踪(影片剪辑是雪碧); // 假
跟踪(Sprite是影片剪辑); // 假
跟踪(Sprite是雪碧); // 假
跟踪(雪碧是Object); // 真正
我一直在努力的下一个code,但它不工作:
/ **
*如果收益类的实例A可强制转换为类B的瞬间
* /
私有函数isInstance(一:类,B:类):布尔{
VAR超:CLASS = A;
做 {
如果(超== b){
返回true;
}
超= getSuperclass之(一);
}而(超!= NULL);
返回false;
}
私有函数getSuperclass之(claz:类):类{
VAR qualifiedSuperclassName:字符串= getQualifiedSuperclassName(claz);
VAR的returnValue:类= getDefinitionByName(qualifiedSuperclassName)为类;
返回的returnValue;
}
解决方案
在此网站上找到解决方案。
这是这么简单:
私有函数isSubclassOfSkyboy(一:类,B:类):布尔
{
如果(INT(一)| INT(B)!)返回false;
回报(A == b || a.prototype的instanceof B);
}
这里有一个使用的instanceof 从AS3 pcated这是德$ P $。据我了解,他无法取代的是是在这种情况下,但纠正我,如果我错了。评论之前的任何方式阅读文章。
I want to implement this method
function isInstance(a:Class, b:Class):Boolean;
This is how AS3 work with Classes. Note that MovieClip extends Sprite.
trace(MovieClip is Sprite); // false
trace(Sprite is MovieClip); // false
trace(Sprite is Sprite); // false
trace(Sprite is Object); // true
I been trying the next code but it is not working:
/**
* return if instance of class 'a' can be cast to instant of class 'b'
*/
private function isInstance(a:Class, b:Class):Boolean{
var superclass:Class = a;
do {
if (superclass == b) {
return true;
}
superclass = getSuperClass(a);
} while (superclass != null);
return false;
}
private function getSuperClass(claz:Class):Class{
var qualifiedSuperclassName:String = getQualifiedSuperclassName(claz);
var returnValue:Class = getDefinitionByName(qualifiedSuperclassName) as Class;
return returnValue;
}
解决方案
Found solution in this site.
It is simple as that:
private function isSubclassOfSkyboy(a:Class, b:Class): Boolean
{
if (int(!a) | int(!b)) return false;
return (a == b || a.prototype instanceof b);
}
There is a use here of instanceof that been deprecated from as3. As I understood he cannot be replaced with is in this case, but correct me if I am wrong. Any way read the article before commenting.