📜  C#中的方法重写和方法隐藏之间的区别

📅  最后修改于: 2021-05-30 00:35:43             🧑  作者: Mango

方法重写是一种允许从派生类中的另一个类(基类)调用函数的技术。在派生类中创建与基类中的方法具有相同签名的方法称为方法重写
简而言之,Overrideing是一项功能,它允许子类或子类提供其超类或父类之一已经提供的方法的特定实现。当子类中的方法与其父类中的方法具有相同的名称,相同的参数或签名以及相同的返回类型(或子类型)时,则称子类中的方法覆盖父类中的方法。 -班级。

例子:

// C# program to illustrate the 
// Method overriding concept
using System;
  
// Base class
class My_Parent {
  
    // virtual method
    public virtual void display()
    {
        Console.WriteLine("My new parent class.. !");
    }
}
  
// Derived class
class My_Child : My_Parent {
  
    // Here display method is overridden
    public override void display()
    {
        Console.WriteLine("My new child class.. !");
    }
}
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        My_Parent obj;
  
        // Creating object of the base class
        obj = new My_Parent();
  
        // Invoking method of the base class
        obj.display();
  
        // Creating object of the derived class
        obj = new My_Child();
  
        // Invoking method of derived class
        obj.display();
    }
}

输出:

My new parent class.. !
My new child class.. !

在“方法隐藏”中,可以使用new关键字从派生类中隐藏基类方法的实现。换句话说,在方法隐藏中,您可以使用new关键字在派生类中重新定义基类的方法。

例子:

// C# program to illustrate the
// concept of method hiding
using System;
  
// Base Class
public class My_Parent {
  
    public void show()
    {
        Console.WriteLine("This is my parent class.");
    }
}
  
// Derived Class
public class My_Child : My_Parent {
  
    // Hide the method of base class
    // Using new keyword
    public new void show() {
  
        Console.WriteLine("This is my child class.");
    }
}
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating the object of 
        // the derived class
        My_Child obj = new My_Child();
  
        // Access the method of derived class
        obj.show();
    }
}

输出:

This is my child class.

方法覆盖与方法隐藏

Method overriding Method hiding
In method overriding, you need to define the method of a parent class as a virtual method using virtual keyword and the method of child class as an overridden method using override keyword. In method hiding, you just simply create a method in a parent class and in child class you need to define that method using new keyword.
It only redefines the implementation of the method. In method hiding, you can completely redefine the method.
Here overriding is an object type. Here hiding is a reference type.
If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method. If you do not use the new keyword, then the compiler will automatically hide the method of the base class.
In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class. In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.