博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 语法练习(10): 类[二] - 继承、覆盖、多态、隐藏
阅读量:7132 次
发布时间:2019-06-28

本文共 1849 字,大约阅读时间需要 6 分钟。

  hot3.png

继承:

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();    }}

转载于:https://my.oschina.net/hermer/blog/320377

你可能感兴趣的文章
正则表达式
查看>>
创建一个MXNET和Scala的Docker环境
查看>>
docker两个容器之间连接---centos7容器+mysql容器
查看>>
/sbin/ldconfig
查看>>
/dev/null与/dev/zero详解
查看>>
详解:Linux 硬盘,格式化,分区 详解
查看>>
刚从eclipse转到Intellij IDEA,分享一些配置经验
查看>>
微博开放平台应用申请(Android签名填写规则)
查看>>
AppDomain.AssemblyResolve Event
查看>>
cucumber之基础概念
查看>>
IPC机制学习---D-BUS
查看>>
VML/SVG开发配电站接线系统
查看>>
hystrix 从Camden SR7升级到Dalston.SR1
查看>>
Android MMS模块数据存取
查看>>
Log4j官方文档翻译(六、日志的级别)
查看>>
JS BOM知识整理
查看>>
Oracle 数据库导入导出 dmp文件
查看>>
浅谈什么是云主机及其优势所在
查看>>
TXLSReadWriteII5 单元格读写
查看>>
.Net 调式案例—实验1 hang复习回顾
查看>>