AS3:访问不在超类中的扩展类上的对象属性或函数

发布于 2024-12-29 18:34:15 字数 830 浏览 0 评论 0原文

如果我有三个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2025-01-05 18:34:15

如果该变量在您的某些子类中使用,但不是在所有子类中使用,并且您尚未在父类中定义它,您仍然可以尝试访问它。我建议进行一些快速转换:

if (ex is AnotherExtenedExample || ex is ExtendedExample)
{
    var tmpex:ExtendedExample = ex as ExtendedExample;
    trace (tmpex.variable);
}

您还可以将其转换为动态对象类型并尝试访问 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:

if (ex is AnotherExtenedExample || ex is ExtendedExample)
{
    var tmpex:ExtendedExample = ex as ExtendedExample;
    trace (tmpex.variable);
}

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.

指尖凝香 2025-01-05 18:34:15

@Lucas将您的代码更改为以下代码

function functionThatReceivesAnExtendedExample (ex:Example):void {
    if(ex is ExtendedExample) {
        (ex as ExtendedExample).func()
    } else if(ex is AnotherExtendedExample)
    {
        (ex as AnotherExtendedExample).funct() 
    }
}

希望这会有所帮助

@Lucas change your code as below code

function functionThatReceivesAnExtendedExample (ex:Example):void {
    if(ex is ExtendedExample) {
        (ex as ExtendedExample).func()
    } else if(ex is AnotherExtendedExample)
    {
        (ex as AnotherExtendedExample).funct() 
    }
}

hope this will help

眼趣 2025-01-05 18:34:15

正如 RIAstar 在他的评论中所说,最明显的方法是让 Example 也有一个 func 函数,并在子类中重写它。

实现一个接口,而不是扩展基类,或者同时执行这两个操作,可能是让 functionThatReceivesAnExtendedExample 调用 ex 上的 func 的另一种方法,而不关心什么ex 对象所属的确切类,并且 Example 类不必实现 func 函数,如您的示例中所示。所以类似这样的事情,基于您的示例代码:

public class Example {
    public function Example () {

    }
}

public interface IFunc {
    function func():void;
}

public class ExtendedExample extends Example implements IFunc {
    public function func ():void {
        //here is what this class does different that the next
    }
}

public class AnotherExtendedExample extends Example implements IFunc {
    public function func ():void {
        //here is what this class does, and it is differente from ExtendedExample
    }
}

function functionThatReceivesAnExtendedExample (ex:IFunc):void {
    ex.func()
}

As RIAstar says in his comments, the most obvious way would be to make Example also have a func 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 call func on ex without caring what exact class the ex object is, and without the Example class having to implement a func function, as in your example. So something like this, building on your example code:

public class Example {
    public function Example () {

    }
}

public interface IFunc {
    function func():void;
}

public class ExtendedExample extends Example implements IFunc {
    public function func ():void {
        //here is what this class does different that the next
    }
}

public class AnotherExtendedExample extends Example implements IFunc {
    public function func ():void {
        //here is what this class does, and it is differente from ExtendedExample
    }
}

function functionThatReceivesAnExtendedExample (ex:IFunc):void {
    ex.func()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文