为什么我可以调用基本构造函数

时间:2019-05-06 20:24:45

标签: c# inheritance constructor

我想从基类继承Constructor。 我需要它来打印/制作新对象。

namespace Zoo
{
    class Animal
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Weigth { get; set; }



        public Animal()
        {
            Name = "nieznane";
            Age = 0;
            Weigth = 0;
        }

        public Animal(string Name,int Age, int Weigth)
        {
            this.Name = Name;
            this.Age = Age;
            this.Weigth = Weigth;
        }

        public void Introduce()

        {
            Console.WriteLine(string.Format("{0} {1}y, {2}kg", Name, Age, Weigth));
        }

    }// class end

    class Mammal : Animal
    {
        public string FurColour { get; set; }


        public Mammal()

        {
            FurColour = "nieznane";
        }

        public Mammal(string furColour)    
        : base( Name,Age,Weigth)  // there is the problem , this dont work
        {
            FurColour = furColour;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您应提供NameAgeWeigth

public Mammal(string Name, int Age, int Weigth, string furColour)    
    : base(Name, Age, Weigth)  
    {
        FurColour = furColour;
    }