//基类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MVATwentyQuestions { class Test { public string _name = ""; public Test(string name) { _name = name; } public virtual void ConvertToUpper() { _name = _name.ToUpper(); } } } //子类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MVATwentyQuestions { class InheritTest : Test { //base("NewName"); //错误的调用,所以注释 //继承基类的构造器 public InheritTest(string name) : base(name) { } //继承基类的构造器,传入该构造器的参数为一个值的方法 public InheritTest(string name) : base(ModifyBase(name)) { } private static string ModifyBase(string newName) { return newName.ToUpper(); } } } //主方法 static void Main(string[] args) { Console.WriteLine(); Test nTest = new Test("Base"); Console.WriteLine(nTest._name); InheritTest iTest = new InheritTest("NewName"); Console.WriteLine(iTest._name); }
继承基类的构造器
结果:
Base
NewName
继承基类的构造器,传入该构造器的参数为一个值的方法
结果:
Base
NEWNAME
5 在C#中如何调用基类构造器
时间: 2024-10-14 10:49:11