as3继承变量

发布于 2024-12-29 11:28:48 字数 783 浏览 0 评论 0原文

我有一个基类“车辆”和一个派生类“汽车”。

package Game
{

    public class Vehicle
    {
        public var myVar = "vehicle";

        public function Vehicle()
        {
            trace("vehicle: " + myVar);
            DoSomethingWithMyVar();
        }

    }

}

package Game
{

    public class Car extends Vehicle
    {

        public function Car()
        {
            trace("car pre: " + myVar);
            myVar = "car";
            trace("car post: " + myVar);
            super();
        }

    }

}

基本上,我想在 Car 的构造函数中设置车辆属性,使用 super() 调用 Vehicle 的构造函数,并让 Vehicle 基于 myVar 执行某些操作。但这是我得到的输出:

car pre: null
car post: car
vehicle: vehicle

为什么 myVar 在调用基类构造函数时不保留“car”的值?我应该如何正确实施这个?

I have a base class "Vehicle" and a derived class "Car".

package Game
{

    public class Vehicle
    {
        public var myVar = "vehicle";

        public function Vehicle()
        {
            trace("vehicle: " + myVar);
            DoSomethingWithMyVar();
        }

    }

}

package Game
{

    public class Car extends Vehicle
    {

        public function Car()
        {
            trace("car pre: " + myVar);
            myVar = "car";
            trace("car post: " + myVar);
            super();
        }

    }

}

Basically, I want to set a vehicle property in Car's constructor, call Vehicle's constructor with super() and have Vehicle do something based on myVar. But this is the output i get:

car pre: null
car post: car
vehicle: vehicle

Why does myVar not keep the value of "car" when the base class constructor is called? How am I supposed to implement this correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦在深巷 2025-01-05 11:28:48

变量初始化在超类中。在您实际调用 super(); 之前,它不会被初始化。这就是为什么您应该在构造函数中执行任何其他操作之前始终调用 super() 。事实上,在其他一些语言(如 Java)中,对 super() 的调用只允许作为子类构造函数中的第一个语句。

但是,您应该考虑在构造函数中使用初始化参数,而不是在声明中设置值 - 这是执行您打算执行的操作的更优雅且可维护的方式。

package game
{   
    public class Vehicle
    {
        public var myVar: String;

        public function Vehicle ( type:String = "vehicle" ) 
                             // new Vehicle() traces "vehicle"
        {
            trace("vehicle: " + type);
            myVar = type;
            doSomethingWithMyVar();
        }
    }
}

分别。

public function Car ( type: String = "car" )
                             // new Car() traces "car"
{
    super( type );
}

另外,您始终可以调用 new Car("Porsche"); 或类似的名称 ;)

PS 旁注:ActionScript 中的命名约定建议对两个包使用以小写字母开头的名称和方法。

The variable initialization is in the super class. It won't be initialized until you actually call super();. This is why you should always call super() before doing anything else in the constructor. In fact, in some other languages, like Java, calls to super() are only allowed as the first statement within subclass constructors.

However, you should consider using an initialization parameter in your constructor instead of setting the value in the declaration - it is the more elegant and maintainable way of doing what you intend to do.

package game
{   
    public class Vehicle
    {
        public var myVar: String;

        public function Vehicle ( type:String = "vehicle" ) 
                             // new Vehicle() traces "vehicle"
        {
            trace("vehicle: " + type);
            myVar = type;
            doSomethingWithMyVar();
        }
    }
}

resp.

public function Car ( type: String = "car" )
                             // new Car() traces "car"
{
    super( type );
}

plus, you can always call new Car("Porsche");, or something like that ;)

P.S. On a side note: Naming conventions in ActionScript recommend using names starting with a lower case letter for both packages and methods.

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