📜  隐藏在C#中的方法

📅  最后修改于: 2021-05-29 19:05:30             🧑  作者: Mango

我们已经知道C#中的多态性和方法覆盖。 C#还提供了一种从派生类中隐藏基类方法的概念,该概念称为“方法隐藏”。也称为“方法阴影” 。在方法隐藏中,可以使用new关键字从派生类中隐藏基类的方法的实现。换句话说,在方法隐藏中,您可以使用new关键字在派生类中重新定义基类的方法。

例子:

// C# program to illustrate the
// concept of method hiding
using System;
  
// Base Class
public class My_Family {
  
    public void member()
    {
        Console.WriteLine("Total number of family members: 3");
    }
}
  
// Derived Class
public class My_Member : My_Family {
  
    // Reimplement the method of the base class
    // Using new keyword
    // It hides the method of the base class
    public new void member() 
    {
        Console.WriteLine("Name: Rakesh, Age: 40 \nName: Somya, "+
                               "Age: 39 \nName: Rohan, Age: 20 ");
    }
}
  
// Driver Class
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating the object of the derived class
        My_Member obj = new My_Member();
  
        // Access the method of derived class
        obj.member();
    }
}

输出:

Name: Rakesh, Age: 40 
Name: Somya, Age: 39 
Name: Rohan, Age: 20 

说明:在上面的示例中, My_Family是基类,而My_Member是派生类。在这两个类中,我们具有相同的名称方法,即成员()方法。但是在派生类中, 成员()方法是使用new关键字声明的。调用此方法时,它将打印家庭成员的姓名和年龄,而不是家庭成员的总数。这意味着当我们在派生类对象的帮助下调用成员()方法时,由于存在new关键字,它会隐藏基类中存在的相同名称的方法。

现在,我们将看到如果不使用new关键字从派生类中隐藏基类的方法,将会发生什么。如下面的示例所示,当我们不使用new关键字时,编译器将给出运行代码而不会给出错误,但是它将发出警告,如果您要隐藏方法,请使用new关键字。

例子:

// C# program to illustrate the
// concept of method hiding
// without using the new keyword
using System;
  
// Base Class
public class My_Family {
  
    public void member()
    {
        Console.WriteLine("Total number of family members: 3");
    }
}
  
// Derived Class
public class My_Member : My_Family {
  
    public void member()
    {
        Console.WriteLine("Name: Rakesh, Age: 40 \nName: Somya, "+
                               "Age: 39 \nName: Rohan, Age: 20 ");
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating the object of the derived class
        My_Member obj = new My_Member();
  
        // Access the method of derived class
        obj.member();
    }
}

警告:

输出:

Name: Rakesh, Age: 40 
Name: Somya, Age: 39 
Name: Rohan, Age: 20 

如何调用隐藏方法?

在方法隐藏中,您还可以使用三种不同的方法在派生类中调用基类的隐藏方法,这些方法是:

  • 通过使用base关键字,可以在派生类中调用基类的隐藏方法,如以下示例所示:

    例子:

    // C# program to illustrate how
    // to access hidden method
    using System;
      
    // Base Class
    public class My_Family {
        public void member()
        {
            Console.WriteLine("Total number of family members: 3");
        }
    }
      
    // Derived Class
    public class My_Member : My_Family {
      
          
        public new void member() 
        {
              
            // Calling the hidden method of the
            // base class in a derived class
            // Using base keyword
            base.member();
            Console.WriteLine("Name: Rakesh, Age: 40 \nName: Somya,"+
                                  " Age: 39 \nName: Rohan, Age: 20");
        }
    }
      
    class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Creating the object of the derived class
            My_Member obj = new My_Member();
      
            // Access the method of derived class
            obj.member();
        }
    }
    

    输出:

    Total number of family members: 3
    Name: Rakesh, Age: 40 
    Name: Somya, Age: 39 
    Name: Rohan, Age: 20
    
  • 通过将派生类类型转换为基类类型,可以调用隐藏方法。如下例所示。我们知道在继承中派生类具有基类的所有功能,因此我们可以轻松地将派生类的对象转换为基类类型。

    例子:

    // C# program to illustrate how
    // to access hidden method
    using System;
      
    // Base Class
    public class My_Family {
      
        public void member()
        {
            Console.WriteLine("Total number of family members: 2");
        }
    }
      
    // Derived Class
    public class My_Member : My_Family {
          
        public new void member() {
      
            Console.WriteLine("Name: Rakesh, Age: 40 "+
                             "\nName: Somya, Age: 39");
        }
    }
      
    class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Creating the object of the derived class
            My_Member obj = new My_Member();
      
            // Invoking the hidden method
            // By type casting
            ((My_Family)obj).member();
        }
    }
    

    输出:

    Total number of family members: 2
    
  • 代替使用派生类引用变量,我们使用父类引用变量来调用隐藏方法。它类似于上述调用隐藏方法的方式。在这里,我们还使用不同的语法键入case派生类的对象。

    注意:如果您尝试使用以下语法调用隐藏方法,

    My_Member obj = new My_Family();

    在这里,编译器将给出错误,因为My_Family类的对象无法履行My_Member类的职责。

    例子:

    // C# program to illustrate how
    // to access hidden method
    using System;
      
    // Base Class
    public class My_Family {
      
        public void member()
        {
            Console.WriteLine("Total number of family members: 2");
        }
    }
      
    // Derived Class
    public class My_Member : My_Family {
      
        public new void member() {
      
            Console.WriteLine("Name: Rakesh, Age: 40 "+
                             "\nName: Somya, Age: 39");
        }
    }
      
    class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Invoking the hidden method
            My_Family obj = new My_Member();
            obj.member();
        }
    }
    

    输出:

    Total number of family members: 2