C# 语法练习(10): 类[二] - 继承、覆盖、多态、隐藏
2009-01-02 16:45
513 查看
继承:
覆盖:
多态:
隐藏:
using System; class Parent { public void Msg() { Console.WriteLine("Parent"); } } class Child : Parent { } class Program { static void Main() { Parent ObjParent = new Parent(); Child ObjChild = new Child(); ObjParent.Msg(); //Parent ObjChild.Msg(); //Parent Console.ReadKey(); } }
覆盖:
using System; class Parent { public virtual void Msg() { Console.WriteLine("Parent"); } } class Child : Parent { public override void Msg() { Console.WriteLine("Child"); } } class Program { static void Main() { Parent ObjParent = new Parent(); Child ObjChild = new Child(); ObjParent.Msg(); //Parent ObjChild.Msg(); //Child Console.ReadKey(); } }
多态:
using System; class Parent { public virtual void Msg() { Console.WriteLine("Parent"); } } class Child1 : Parent { public override void Msg() { Console.WriteLine("Child_1"); } } class Child2 : Parent { public override void Msg() { Console.WriteLine("Child_2"); } } class Program { static void Main() { Parent Obj1 = new Child1(); Parent Obj2 = new Child2(); Obj1.Msg(); //Child_1 Obj2.Msg(); //Child_2 Console.ReadKey(); } }
隐藏:
using System; class Parent { public void Msg() { Console.WriteLine("Parent"); } } /* 有意隐藏应使用 new 关键字 */ class Child1 : Parent { new public void Msg() { Console.WriteLine("Child_1"); } } /* 无意隐藏会有提示, 但可用 */ class Child2 : Parent { public void Msg() { Console.WriteLine("Child_2"); } } class Program { static void Main() { Parent Obj1 = new Child1(); Parent Obj2 = new Child2(); Child1 Obj3 = new Child1(); Child2 Obj4 = new Child2(); Obj1.Msg(); //Parent Obj2.Msg(); //Parent Obj3.Msg(); //Child_1 Obj4.Msg(); //Child_2 Console.ReadKey(); } }
相关文章推荐
- C#继承、覆盖和多态、抽象类
- 分析JAVA、C#、C++的“覆盖”和“隐藏”与多态的实现
- C++ 封装、继承、多态、重载、覆盖、隐藏基本概念详解
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- C++继承、虚函数、纯虚函数、多态、覆盖、重载、隐藏、引用等常用概念
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- [原创]C#中的继承、重写、覆盖、多态、virtual override 以及其他
- [Unity&C#&继承]unity继承中覆盖和隐藏基类成员变量 string 变量
- C#学习笔记:多态与隐藏,覆盖
- Java基本功练习十六(继承和多态【继承多态覆盖练习、多重继承、数组线性表ArrayList、银行账户类升级】)
- 总结:C#语法——继承、虚拟、多态、实例化
- [Java] 继承,隐藏,覆盖,重载,多态,抽象类,接口
- C#中的继承、重写、覆盖、多态、virtual override 以及其他
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- c#重写,覆盖,隐藏,重载,继承
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- 继承与多态---上 --- 函数重写、多态、虚函数、覆盖、隐藏
- C# 基础系列--继承二(多态是不是重载)
- Java 类总结(多态,静态绑定,动态绑定,重写,覆盖,隐藏)