1 using System; 2 3 public class Parent 4 { 5 string parentString; 6 public Parent() 7 { 8 Console.WriteLine("Parent Constructor."); 9 }10 public Parent(string myString)11 {12 parentString = myString;13 Console.WriteLine(parentString);14 }15 public void print()16 {17 Console.WriteLine("I'm a Parent Class.");18 }19 }20 21 public class Child : Parent22 {23 public Child() : base("From Derived")24 {25 Console.WriteLine("Child Constructor.");26 }27 public new void print()28 {29 base.print();30 Console.WriteLine("I'm a Child Class.");31 }32 public static void Main()33 {34 Child child = new Child();35 child.print();36 ((Parent)child).print();37 }38 }
注意new,base。