Does code in constructor add code to subclass constructors?

Does code in constructor add code to subclass constructors? Or does the subclass constructor override the superclass? Given this example of a superclass constructor:

class Car{
     function Car(speed:int){ 
          trace("CAR speed "+speed)
     }
}

... and this subclass constructor:

class FordCar extends Car{
     function FordCar(model:string){ 
          trace("FORD model "+model)
     }
}

When an instance is created FordCar, will this trace be "Automobile" and "Ford" ??

Edit: Can arguments be added? See updated code above.

+5
source share
4 answers

Yes. In most languages, the base class constructor is executed, then the subclass constructor. This will make him trace: “CAR”, then “FORD”.

, . , , . ( , - .)


Edit:

. . , :

function FordCar(model: string) : Car(42) {

( ) , , .

:

function FordCar(int: speed, model: string) : Car(speed) {
    trace("FORD model " + model)
}
+6

, . . , . , , .

, :

public interface IAnimal
{
    string GetName();
    string Talk();
}

public abstract class AnimalBase : IAnimal
{
    private string _name;

    // Constructor #1
    protected AnimalBase(string name)
    {
        _name = name;
    }

    // Constructor #2
    protected AnimalBase(string name, bool isCutsey)
    {
        if (isCutsey)
        {
            // Change "Fluffy" into "Fluffy-poo"
            _name = name + "-poo";
        }
    }

    // GetName implemention from IAnimal.
    // In C#, "virtual" means "Let the child class override this if it wants to but is not required to"
    public virtual string GetName()
    {
        return _name;
    }

    // Talk "implementation" from IAnimal.
    // In C#, "abstract" means "require our child classes to override this and provide the implementation".
    // Since our base class forces child classes to provide the implementation, this takes care of the IAnimal implementation requirement.
    abstract public string Talk();
}

public class Dog : AnimalBase
{
    // This constructor simply passes on the name parameter to the base class constructor.
    public Dog(string name)
        : base(name)
    {
    }

    // This constructor passes on both parameters to the base class constructor.
    public Dog(string name, bool isCutsey)
        : base(name, isCutsey)
    {
    }

    // Override the base class Talk() function here, and this satisfy AnimalBase requirement to provide this implementation for IAnimal.
    public override string Talk()
    {
        return "Woof! Woof!";
    }
}

public class SmallDog : Dog
{
    private bool _isPurseDog;

    // This constructor is unique from all of the other constructors.
    // Rather than the second boolean representing the "isCutsey" property, it entirely different.
    // It entirely a coincidence that they're the same datatype - this is not important.
    // Notice that we're saying ALL SmallDogs are cutsey by passing a hardcoded true into the base class (Dog) second parameter of the constructor.
    public SmallDog(string name, bool isPurseDog)
        : base(name, true)
    {
        _isPurseDog = isPurseDog;
    }

    // This tells us if the dog fits in a purse.
    public bool DoesThisDogFitInAPurse()
    {
        return _isPurseDog;
    }

    // Rather than using Dog Talk() implementation, we're changing this because this special type of dog is different.
    public override string Talk()
    {
        return "Yip! Yip!";
    }
}

public class Chihuahua : SmallDog
{
    private int _hatSize;

    // We say that Chihuahua always fit in a purse. Nothing else different about them, though.
    public Chihuahua(string name, int hatSize)
        : base(name, true)
    {
        _hatSize = hatSize;
    }

    // Of course all chihuahuas wear Mexican hats, so let make sure we know its hat size!
    public int GetHatSize()
    {
        return _hatSize;
    }
}

public class Cat : AnimalBase
{
    // This constructor simply passes on the name parameter to the base class constructor.
    public Cat(string name)
        : base(name)
    {
    }

    // This constructor passes on both parameters to the base class constructor.
    public Cat(string name, bool isCutsey)
        : base(name, isCutsey)
    {
    }

    // Override the base class Talk() function here, and this satisfy AnimalBase requirement to provide this implementation for IAnimal.
    public override string Talk()
    {
        return "Meoooowwww...";
    }
}

public class Lion : Cat
{
    public Lion(string name)
        : base(name)
    {
    }

    // Rather than using Cat Talk() implementation, we're changing this because this special type of cat is different.
    public override string Talk()
    {
        return "ROAR!!!!!!!!";
    }
}

[]

, , . . . . , , . , true/false.

+4

# ( ) . , , .

+1

, : Python - .

class Car():
  def __init__(self, speed):
    print "speed", speed
    self.speed = speed
class FordCar(Car):
  def __init__(self, model):
    print "model", model
    self.speed = 180
    self.model = model

>>> FordCar("Mustang")
model Mustang

imvho, , Python .

+1

All Articles