简体   繁体   English

为什么在c#的继承方法中使用override而不是function

[英]Why to use override in Inheritance method of c# instead of function

I wanted to know that while inheritance can be used to inherit properties and methods in derived class. 我想知道虽然继承可以用来继承派生类中的属性和方法。

Why we don't write functions in the derived classes instead declaring them as override. 为什么我们不在派生类中编写函数而是将它们声明为覆盖。

Following example will explain more about my question. 以下示例将详细解释我的问题。

class Net
{
    public virtual void Act()
    {
    }
}

class Perl : Net
{
    public override void Act()
    {
    Console.WriteLine("Perl.Act");
    }
}

class Python : Net
{
    public override void Act()
    {
    Console.WriteLine("Python.Act");
    }
}
// Use base class and derived types in a List.
List<Net> nets = new List<Net>();
nets.Add(new Perl());
nets.Add(new Python());

// Call virtual method on each instance.
foreach (Net net in nets)
{
    net.Act();
}

In above example act function can be declared as "NEW" functions why should we opt for overriding the base function?? 在上面的示例中,act函数可以声明为“NEW”函数,为什么我们要选择覆盖基函数?

Edit: Suppose i have a case as follow 编辑:假设我有一个案例如下

class Net
{
    public virtual void Act()
    {
    }

    public virtual void secondMethod()
    {
    }
}

class Perl 
{
    public override void Act()
    {
        Console.WriteLine("Perl.Act");
    }

    public override void secondMethod()
    {
        Console.WriteLine("Perl.Act");
    }
}

class Python 
{
    public new void Act()
    {
        Console.WriteLine("Python.Act");
    }

    public override void secondMethod()
    {
        Console.WriteLine("Perl.Act");
    }
}

What should i do then. 那我该怎么办 In above case i want the base class to be inherited but The functionality i am implementing may be different. 在上面的例子中,我希望继承基类,但我实现的功能可能不同。 In such cases should i use override or write a complete different method. 在这种情况下,我应该使用覆盖或写一个完全不同的方法。

Because without that virtual method on your base class you would not be able to call Act withing your loop: 因为在您的基类上没有该virtual方法,您将无法使用循环调用Act

foreach (Net net in nets)
{
    net.Act();
}

And foreach is not the only place you can use that. 并且foreach不是唯一可以使用它的地方。 Imagine you have a method called Foo which takes Net instance as parameter: 想象一下,你有一个名为Foo的方法,它将Net实例作为参数:

public static void Foo(Net net)
{
    net.Act();
}

You can still pass both Python and Perl to that method, and they will both execute their own Act method logic. 您仍然可以将PythonPerl都传递给该方法,并且它们都将执行自己的Act方法逻辑。

virtual is useful everywhere you want to provide some basic, default functionality and be able to override that default one within derived classes. virtual在您想要提供一些基本的默认功能的任何地方都很有用,并且能够在派生类中override该默认功能。

You should read Knowing When to Use Override and New Keywords (C# Programming Guide) 您应该阅读了解何时使用覆盖和新关键字(C#编程指南)

new keyword is used for method hiding, mainly for versioning purposes, but note that both methods are still available in derived class! new关键字用于方法隐藏,主要用于版本控制 ,但请注意,这两个方法在派生类中仍然可用! and can be accessed with a simple cast . 并且可以通过简单的cast访问。 But on the other hand, with method overriding, you override the behavior (this is what we call polymorphism ), so in a derived class, the overridden method will totally replace the base method, therefore cannot be accessed via an instance of the derived class even with an upcast to the base class! 但另一方面,使用方法覆盖,您覆盖行为(这就是我们所说的多态 ),因此在派生类中,重写的方法将完全替换基本方法,因此无法通过派生类的实例进行访问即使是对基层的向上倾斜! because the virtual method is replaced by the overridden method. 因为虚方法被重写方法替换。 Let me show you this concept with an example: 让我用一个例子向您展示这个概念:

public class Base
{
    public virtual void VirtualMethod() { }
    public void NewMethod() { }
}

public class Derived : Base
{
    public override void  VirtualMethod() { }
    public new void NewMethod() { }
}

...

Base derived = new Derived();
derived.VirtualMethod(); // Derived.VirtualMethod()
derived.NewMethod(); // Base.NewMethod()
((Derived)derived).NewMethod(); // Derived.NewMethod()

In above example act function can be declared as "NEW" functions why should we opt for overriding the base function 在上面的示例中,act函数可以声明为“NEW”函数,为什么我们应该选择覆盖基函数

Inheritance is meant to use what base class provide along with extra functionality where necessary. 继承旨在使用基类提供的内容以及必要时的额外功能。 Why would you inherit in first place if you don't want to use the functionality of base class? 如果你不想使用基类的功能,为什么你会在第一时间继承?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM