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



  • 相关阅读:
    【BZOJ3998】弦论(后缀自动机)
    【BZOJ4566】找相同字符(后缀自动机)
    AtomicInteger在实际项目中的应用
    ElasticSearchserver操作命令
    照猫画虎学gnuplot之折线图
    HDU 5667 :Sequence
    mycat 连续分片 -> 按日期(天)分片
    DM8168 unrecoverable error: OMX_ErrorBadParameter (0x80001005) [resolved]
    多线程(一):初识多线程
    Vb.net/VB 声明API函数实现父窗口功能
  • 原文地址:https://www.cnblogs.com/shuaigezhaoguang/p/5869753.html
Copyright © 2011-2022 走看看