C#调用父类的父类的方法,甚至祖父类的函数

C#怎么调用父类甚至祖父类的虚函数

在项目开发的时候,有类的继承关系,但是,有时候我们就是需要调用父类或祖父类的方法,怎么办呢?

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication2
{
    class Program
    {
        class S1
        {
            public virtual void Method()
            {
                Console.WriteLine("这是S1实例方法");
            }
        }
 
        class S2:S1
        {
            public virtual void Method()
            {
                Console.WriteLine("这是S2实例方法");
            }
        }
 
        class S3 : S2
        {
            public  virtual void Method()
            {
                Console.WriteLine("这是S3实例方法");
            }
        }
 
        static void Main(string[] args)
        {
            S3 s = new S3();
            // 调用S3.Methoed
            s.Method();
 
            // 调用S2.Methoed
            ((S2)s).Method();
 
            // 调用S1.Methoed
            ((S1)s).Method();
        }
    }
}
复制代码

运行结果

 

 

出处:https://zhidao.baidu.com/question/583835539787659325.html

=======================================================================================

C#调用父类的父类的方法

override一个C#函数时,如果想调用这个函数在父类的父类中相应的方法,可以将override关键字变成new,调用时将Object转义成父类的父类

class A
{
 public virtual void Func()
 {
  Console.WriteLine("A");
 }
}


class B:A
{
 public new void Func()
 {
  Console.WriteLine("B");
 }
}


class C:B
{
 public void Func()
 {
  A a = this as C;
  a.Func();   
 }
}

 

 

出处:https://www.cnblogs.com/mbskys/articles/643452.html

=======================================================================================

个人使用

 以上方法,都是使用的public修饰符的,如果是 protected 或 private 修饰符的怎么办呢?

我是在父类中使用了protected 的变量,使子类可以访问,如下:

 

复制代码
    class A
    {
        protected virtual List<Class1> Func()
        {
            Console.WriteLine("A");
            return new List<Class1>();
        }
    }

    class B : A
    {
        protected List<Class1> BaseList = null;
        protected override List<Class1> Func()
        {
            BaseList = base.Func();
            // todo : 对BaseList数据加工处理,并返回新的List<Class1>
            return new List<Class1>();
        }
    }

    class C : B
    {
        protected override List<Class1> Func()
        {
            var aList = BaseList;
            var bList = base.Func();
            // todo : 再次对List<Class1>数据加工处理
            return new List<Class1>();
        }
    }
复制代码

 

posted on   jack_Meng  阅读(2837)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-12-02 JS三座大山再学习 ---- 作用域和闭包
2019-12-02 JS三座大山再学习 ---- 异步和单线程
2019-12-02 JS三座大山再学习 ---- 原型和原型链
2016-12-02 使用Excel制作万年历(日历可A4纸打印)
2015-12-02 IronPython for ASP.NET 部署注意事项
2015-12-02 IronPython+Anthem.Net也玩 Ajax!
2015-12-02 逐步改用 IronPython 开发你的 ASP.NET 应用程序

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩