zoukankan      html  css  js  c++  java
  • c#重写,覆盖,隐藏,重载,继承

    1.重写和覆盖:在c#中这两个是一个意思,重写就是覆盖,覆盖就是重写(在c++中是两个不同的概念)

    2.隐藏:隐藏就是子类与父类的方法一样(子类函数名前加一个new),并且子类不是虚方法,那么子类就会隐藏父类方法

    3.重载:方法名相同,参数列表不同

    4.继承:c#中不允许多继承(接口可以)

    下面看具体事例

    1.重写(父类用virtual修饰(虚函数),子类用override修饰)
    class Animal
        {
            
            public virtual void Voice()
            {
                Console.WriteLine ("Animal");
            }
        }
    class House:Animal
        {
            public override void Voice()
            {
                Console.WriteLine ("House");
            }
        }
    2.隐藏
    子类可以隐藏父类的方法,不过在c#中会发出警告,添加一个new就ok了
    使用new修饰,可以隐藏同名函数。

    class ClassA//没有指定继承,则默认继承object
        {
            
            public void Method()
            {
                Console.WriteLine ("classA Method");
            }
        }
        class ClassB:ClassA
        {
           
            public new void Method()
            {
                Console.WriteLine ("classB Method");
            }
        }

    3.重载(和c++一致)
    参数列表不同包括:
    1.参数个数相同,参数类型不同
    2.参数类型相同,参数个数不同
    原函数:

    public void print(int x,int y){
       Console.WriteLine (x+y);
    }

    重载的函数
    public void print(string s,string s2){
        Console.WriteLine (s+s2);
    }


    public void print(int x,int y,int z){
         Console.WriteLine (x+y+z);
    }



  • 相关阅读:
    RestTemplate与Gzip压缩
    在浏览器中异步下载文件监听下载进度
    springBoot中的所有配置属性(中文)
    Springboot应用中设置Cookie的SameSite属性
    客户端解析服务器响应的multipart/form-data数据
    springboot + querydsl 入门到会用
    MyBatis通过TypeHandler自动编解码对象的Json属性
    @Transaction注解失效的几种场景
    Elasticsearch 7.x配置用户名密码访问 开启x-pack验证
    搭建Elasticsearch可视化界面 Kibana
  • 原文地址:https://www.cnblogs.com/shuaigezhaoguang/p/5869753.html
Copyright © 2011-2022 走看看