简体   繁体   English

从派生类对象访问重写的方法

[英]accessing overridden method from derived class object

Is it possible to access the method that has been overridden using the object of the derived class?是否可以使用派生类的对象访问已被覆盖的方法?

using System;


class Bclass
{
    public virtual int result(int a,int b)
    {
        return a + b;
    }
}
class Dclass:Bclass
{
    public override int result(int a, int b)
    {
        return a - b;
    }
}

public class Program
{

    static public void Main(string[] args)
    {
        Dclass obj1 = new Dclass();
        Console.WriteLine(obj1.result(10, 5));
    }

}

Is there a way to get the output as 15?有没有办法让输出为15?

From MSDN documentation:来自MSDN 文档:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.需要覆盖修饰符来扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现。


override modifier are designed to extend functionality of virtual function. override修饰符旨在扩展virtual函数的功能。 When ever you call overridden function with the help of derived class object it will call overridden function.当您在派生类对象的帮助下调用覆盖函数时,它将调用覆盖函数。

To answer your question,要回答你的问题,

Is it possible to access the method that has been overridden using the object of the derived class?是否可以使用派生类的对象访问已被覆盖的方法?

  • There is no way to call virtual method directly using derived class object没有办法直接使用派生类对象调用虚方法

Ways to call virtual method:调用虚方法的方法:

Approach 1:方法一:

Create a new function in derived class and call base.result() from it.在派生类中创建一个新函数并从中调用base.result()

public int BaseResult(int a, int b)
{
    return base.result(a, b);
}

and call BaseResult() using derived class instance并使用派生类实例调用BaseResult()

  Dclass obj1 = new Dclass();
  Console.WriteLine(obj1.BaseResult(10, 5));

Try it online网上试试

Approach 2:方法二:

Create instance of base class and from that access virtual method.创建基类的实例并从该访问virtual方法。

    Bclass obj2 = new Bclass();
    Console.WriteLine(obj2.result(10, 5));

Unless you need to declare the base method as virtual for some reason, you could also achieve what you want by declaring the derived class method as new , thereby hiding the implementation when using it through an instance of the derived class.除非出于某种原因需要将基方法声明为virtual方法,否则您还可以通过将派生类方法声明为new来实现您想要的,从而在通过派生类的实例使用它时隐藏实现。

At the same time, you can still access the base implementation by casting the object to the base class.同时,您仍然可以通过将对象强制转换为基类来访问基实现。

Following the example you provided:按照您提供的示例:

class Bclass
{
    public int result(int a,int b)
    {
        return a + b;
    }
}

class Dclass : Bclass
{
    public new int result(int a, int b)
    {
        return a - b;
    }

    public int BaseResult(int a, int b)
    {
        return base.result(a, b);
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Dclass obj1 = new Dclass();
        Console.WriteLine(((Bclass)obj1).result(10, 5)); // 15
    }
}

More information about the differences between override and new can be found in this MSDN article .可以在这篇MSDN 文章中找到有关overridenew之间差异的更多信息。

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

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