[英]AS3 Class inheritance problem
请帮助Stackoverflow! 我在AS3中有一个MapInterface类,它继承自Interface类。
public class Interface extends Sprite {
public function Interface(){
// do stuff
}
}
接着
import com.georgecrabtree.Interface;
public class MapInterface extends Interface {
public function MapInterface(){
addMapButtons();
}
public function addMapButtons():void {
trace("init");
}
}
一切正常,当我从文档类创建一个新的MapInterface类时,它会跟踪init。 但是,当我尝试称呼它为:
var mapInterface:MapInterface = new MapInterface();
mapInterface.addMapButtons();
从主时间轴我得到这个错误:
1061: Call to a possibly undefined method addMapButtons through a reference with static type com.georgecrabtree:Interface.
预先感谢您的帮助,乔治
如果我正确理解了该问题,则认为您的第二个代码示例有错字。 我想这实际上是:
var mapInterface:Interface = new MapInterface();
mapInterface.addMapButtons();
问题在于您的变量的类型为Interface
,它没有定义addMapButtons()
方法。 您可以通过将变量类型更改为MapInterface或通过向接口添加addMapButtons()
方法并覆盖addMapButtons()
方法来解决此问题。
大家好,感谢您的帮助和关注。 经过一夜安眠,我终于找到了我的问题。 显然,我上面发布的代码是我实际拥有的精简版本。
问题是我的文档类没有扩展Sprite,它扩展了名为Installation的类,而该类本身扩展了Sprite。 这是因为我要进行10个安装,并且它们都有很多共同点,它们都具有屏幕保护程序,介绍性视频等。它们也都具有界面,这是我的问题所在,因为其中有9个安装使用相同的界面,但一个界面(地图安装)需要具有一些额外的GUI元素。
我的问题是我的Installation基类看起来像这样(显示的是精简版本):
public class Installation extends Sprite {
protected var _interface:Interface;
public function Installation(){
//do stuff
}
}
因此,当我的MapInstallation扩展此类时,它继承了_interface变量。 所以当我创建_interface变量作为MapInterface类型时。
_interface = new MapInterface();
..编译器从安装类型中识别出新的MapInterface,但是_interface变量仍被强类型化为Interface,因此当我后来尝试在_interface上调用addMapButtons()函数时,尽管该编译器在那里有其他想法。 我的解决方案是使用铸造。
(_interface as MapInterface).addMapButtons();
一切都很好用。 谢谢您的帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.