补充一点子类调用父类构造器
子类
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { /// <summary> /// 胡萝卜类 /// </summary> class Carrot : Vegetables { public static void Main(string[] args) { Carrot c = new Carrot("aaa"); } //public Carrot() { } public Carrot(string var) : base(var) { }//将传给子类构造器的参数传给父类构造器 } }
父类
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { /// <summary> /// 蔬菜类 /// </summary> class Vegetables { /// <summary> /// 维生素 /// </summary> private string _vitamin; public string Vitamin { get => _vitamin; set => _vitamin = value; } public Vegetables(string var) { this.Vitamin = var; Console.WriteLine("调到我这儿来了并且传的参数是:{0}",var); } //public Vegetables() { } } }
结果: