C# 继承构造函数 child 和 parent ??

sat*_*res 3 c# inheritance constructor

我是 C++ 程序员,我是 C# 新手,我写了一个小程序来测试继承,所以这里是源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lesson3_Class_inherit_
{
   public class Personne
    {
        public string Name;
        public int Age;
        public Personne() { }
        public Personne(string _Name, int _Age) 
        {
            Name = _Name;
            Age = _Age;
            Console.WriteLine("Constrcut Personne Called\n");

        }
        ~Personne() 
        {
            Console.WriteLine("Destruct Personne Called\n");
        }


    };
    class Humain :  Personne 
    {
        public string Langue;
        public Humain(string _Name, int _Age,string _Langue)
        {
        Console.WriteLine("Constrcut Humain Called\n");
         Name = _Name;
         Age = _Age;
         Langue =_Langue;
        }



    };

    class Program
    {
        static void Main(string[] args)
        {
            Humain H1 = new Humain("majdi", 28, "Deutsch");

            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:构造 Humain Called\ 并且没有调用 Personne 类的构造,为什么!!!在 C++ 中,首先调用父类构造函数!!请帮忙 !

小智 6

在 C# 中,您必须使用 base 关键字显式调用父构造函数。所以Humain看起来像

class Humain :  Personne 
    {
        public string Langue;
        public Humain(string _Name, int _Age,string _Langue) : base(_Name, _Age)
        {
         Console.WriteLine("Constrcut Humain Called\n");
         Name = _Name;
         Age = _Age;
         Langue =_Langue;
        }



    };
Run Code Online (Sandbox Code Playgroud)