AS3:访问不在超类中的扩展类上的对象属性或函数
如果我有三个类:
public class Example {
public function Example () {
}
}
public class ExtendedExample extends Example {
public function func ():void {
//here is what this class does different that the next
}
public function ExtendedExample () {
}
}
public class AnotherExtendedExample extends Example {
public function funct ():void {
//here is what this class does, and it is differente from ExtendedExample
}
public function AnotherExtendedExample () {
}
}
您可以看到最后两个类扩展了第一个类,并且都具有“变量”属性。如果我有一个 Example 实例,并且我确信它也是一个 ExtendedExample 或 AnotherExtendedExample 实例,是否有某种方法可以访问“变量”属性?像这样的东西
function functionThatReceivesAnExtendedExample (ex:Example):void {
if(some condition that I may need) {
ex.func()
}
}
If I have three classes:
public class Example {
public function Example () {
}
}
public class ExtendedExample extends Example {
public function func ():void {
//here is what this class does different that the next
}
public function ExtendedExample () {
}
}
public class AnotherExtendedExample extends Example {
public function funct ():void {
//here is what this class does, and it is differente from ExtendedExample
}
public function AnotherExtendedExample () {
}
}
You can see that the two last classes extend the first one, and both have the 'variable' property. In case that I have an instance of Example and I am sure it is also an ExtendedExample OR AnotherExtendedExample instance, is there some way to access the 'variable' property? Something like
function functionThatReceivesAnExtendedExample (ex:Example):void {
if(some condition that I may need) {
ex.func()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果该变量在您的某些子类中使用,但不是在所有子类中使用,并且您尚未在父类中定义它,您仍然可以尝试访问它。我建议进行一些快速转换:
您还可以将其转换为动态对象类型并尝试访问 try..catch 块中的属性。我建议使用像上面这样的转换,这样逻辑更容易遵循。
如果该变量在所有子类中都使用,只需在父类中定义它,并在每个子类中赋予它特定的值即可。
If the variable is used in some of your subclasses, but not in all of them, and you haven't defined it in the parent class, you can still try to access it. I'd suggest some quick casting:
You can also cast it to a dynamic Object type and attempt to access the property in a try..catch block. I'd recommend using casting like above where the logic is easier to follow.
If the variable is used in all subclasses, just define it in the parent class and give it a specific value in each subclass.
@Lucas将您的代码更改为以下代码
希望这会有所帮助
@Lucas change your code as below code
hope this will help
正如 RIAstar 在他的评论中所说,最明显的方法是让
Example
也有一个func
函数,并在子类中重写它。实现一个接口,而不是扩展基类,或者同时执行这两个操作,可能是让
functionThatReceivesAnExtendedExample
调用ex
上的func
的另一种方法,而不关心什么ex
对象所属的确切类,并且Example
类不必实现func
函数,如您的示例中所示。所以类似这样的事情,基于您的示例代码:As RIAstar says in his comments, the most obvious way would be to make
Example
also have afunc
function, and override it in the sub classes.Implementing an interface, instead of extending a base class, or doing both, could be another way to let
functionThatReceivesAnExtendedExample
callfunc
onex
without caring what exact class theex
object is, and without theExample
class having to implement afunc
function, as in your example. So something like this, building on your example code: