public class ParentClass { public ParentClass() { } public string NamePropety { get; set; } public void GetName() { } } public class ChildClass:ParentClass { public ChildClass() { } public int Age { get; set; } public int GetAge() { return 10; } } ParentClass parent = new ParentClass(); //parent.NamePropety //parent.GetName(); //子类转父类。 //ParentClass parent1 = new ChildClass(); 或者ParentClass parent1 = new ChildClass() as ParentClass; //parent1.NamePropety //parent1.GetName(); ChildClass child = new ChildClass(); //child.NamePropety //child.GetName(); //child.GetAge(); //child.Age; //父类转子类。 //child1为NUll对象。 //ChildClass child1 = new ParentClass() as ChildClass; 或者 ChildClass child1 = (ChildClass)new ParentClass(); //Console.WriteLine(child1.NamePropety); //child1.GetName(); //child1.GetAge(); //child1.Age;
C#基础知识—父类和子类的关系
时间: 2024-10-21 00:14:30