【问题标题】:AS3 Inheritance and Parameters IssuesAS3 继承和参数问题
【发布时间】:2013-01-28 02:50:24
【问题描述】:

我在继承方面遇到问题,因为我从未在 ActionScript 3 中这样做过。

请告诉我在这种情况下该怎么办?

假设我有以下课程

package
{
    public class animal
    {
        var age;
        var amountOfLegs;
        var color;
        public function animal(a,b,c)
        {
            age=a;
            amountOfLegs=b;
            color=c;
        }
    }
 }

然后,我想做一个派生类

package
{
    public class cat extends animal
    {
        var hairType;
        public function cat(a,b,c,d)
        {
            age=a;
            amountOfLegs=b;
            color=c;
            hairType=d;
        }
    }
}

为什么我不能像这样让类“猫”? 有人请解释我如何继承一个类并仍然满足它的参数。 我迷路了。 谢谢。

【问题讨论】:

    标签: actionscript-3 class inheritance parameters


    【解决方案1】:

    在你的 cat 类中,替换:

    age=a;
    amountOfLegs=b;
    color=c;
    

    super(a, b, c);
    

    这会调用基类/超类的构造函数,传入 a,b,c。

    【讨论】:

      【解决方案2】:

      您需要使用super 来调用父类的构造函数并传入您的值。

      http://www.emanueleferonato.com/2009/08/10/understanding-as3-super-statement/

      考虑这个例子

      //this class defines the properties of all Animals
      
      public class Animal{
      
          private var _age:int;
          private var _amountOfLegs:int;
          private var _color:String;
      
          public function Animal(age:int, amountOfLegs:int, color:String){
               _age = age;
               _amountOfLegs = amountOfLegs;
               _color = color;
          }
      
      
          public function traceMe():void{
      
               trace("age: " + _age + "legs: " + _amountOfLegs + " color: " + _color);
          }
      
      }
      
      //this makes a cat
      public class Cat extends Animal{
         public function Cat(){
              //going to call the super classes constructor and pass in the details that make a cat
              super(5, 4, "black");
              traceMe(); //will print age: 5 legs: 4 color: black
         }
      
      }
      

      更多阅读:

      http://active.tutsplus.com/tutorials/actionscript/as3-101-oop-introduction-basix/

      【讨论】:

      • -1 in AS3 super 由运行时调用,无论你是否调用它。再加上您忽略了 cat 和 animal 构造函数具有不同数量的参数的实际问题。
      • @The_asMan 没有理由 cat 和 animal 不能有不同的构造函数。此解决方案将为操作员提供他正在寻找的内容。
      • op 所要求的全部是关于多态性,在这种情况下是类型转换 Cat 和 IAnimal 的能力。一篇更好的文章会在这里developria.com/2009/09/as3-object-oriented-concepts-p.html
      猜你喜欢
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 2023-03-11
      • 2023-04-06
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 2015-04-23
      相关资源
      最近更新 更多