简体   繁体   English

C#中是否有方法继承?

[英]Is there Method Inheritance in C#

I am wondering whether there is any feature like method inheritance rather than whole class inheritance, let me elaborate what I am trying to explain : 我想知道是否有任何功能,如方法继承而不是整个类继承,让我详细说明我想解释的内容:

class a {
   public void GetA(){
     // some logic here
}
}

class b {
    public void GetB() : new Class().GetA()
}

I know it looks weird but I was reading how to do delegation in object composition pattern and I thought this pattern for some reason. 我知道它看起来很奇怪,但我正在阅读如何在对象组合模式中进行委托,我出于某种原因认为这种模式。

A common way to do composition is to create a private member variable in class b, of type class a, and in GetB() call a.GetA(). 进行组合的常用方法是在类b中创建类型为a的私有成员变量,并在GetB()中调用a.GetA()。 For example: 例如:

class a {
   public void GetA(){
     // some logic here
   }
}

class b {
    private a _a=new a();

    public void GetB()
    {
         _a.GetA();
    } 
}

Another option might be to define a delegate member variable called GetB instead of simple method and allow the calling code to supply the code that is executed by GetB(). 另一种选择可能是定义一个名为GetB而不是简单方法的委托成员变量,并允许调用代码提供由GetB()执行的代码。

If you just want to call GetA() inside of GetB(), but don't want to define or explicitly reference an instance of class a in GetB(), you can pass GetA() in as a delegate. 如果您只想在GetB()中调用GetA(),但不想在GetB()中定义或显式引用类a的实例,则可以将GetA()作为委托传递。

In C#, there are many predefined delegates such as Action and Func . 在C#中,有许多预定义的委托,例如ActionFunc Or, you could always roll your own delegate to match the method signature. 或者,您可以随时滚动自己的委托以匹配方法签名。

    class a
    {
        public void GetA()
        {
            Console.WriteLine("Hello World!");
        }
    }

    class b
    {
        // No explicit reference to class a of any kind.
        public void GetB(Action action)
        {
            action();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var a = new a();
            var b = new b();

            b.GetB(a.GetA);
        }
    }

The closest thing I can think of is how in C# you can "inherit" a class's constructor in that of its child class (or an overloaded constructor of the same class), like so: 我能想到的最接近的事情是如何在C#中“继承”一个类的构造函数,而不是它的子类(或同一类的重载构造函数),如下所示:

class Animal {
    public string Species { get; set; }

    public Animal(string species) {
        Species = species;
    }
}

class Human : Animal {
    public string Name { get; set; }

    public Human(string name) : base("Homo sapien") {
        Name = name;
    }
}

The behavior of the above code is pretty straightforward: the constructor for the Human class essentially calls the constructor for the Animal class before doing anything else. 上面代码的行为非常简单: Human类的构造函数在执行任何其他操作之前基本上调用了Animal类的构造函数。 Why this same functionality isn't available to non-constructor methods, I'm not sure. 为什么非构造函数方法无法使用相同的功能,我不确定。

This is not possible. 这是不可能的。 If you want b.GetB to reuse the functionality of a.GetA then you need to instantiate an a and invoke a.GetA . 如果你想b.GetB重用的功能a.GetA那么你需要实例化a并调用a.GetA

A slightly different approach would be to accept an instance of ClassA in the constructor of ClassB , rather than instantiating it directly. 稍微不同的方法是在ClassB的构造函数中接受ClassA的实例,而不是直接实例化它。 This would be applying the Dependency Inversion Principle to @Ash's answer: 这将把依赖倒置原则应用于@ Ash的答案:

public class ClassB
{
    private readonly ClassA _a;

    public b(ClassA a)
    {
        _a = a;
    }

    public void GetB()
    {
        _a.GetA();

        // Other logic
    }
}
public class Article
{

 public object AddOrUpdate(params object[] paras){

   return null;

  }

}

public class Test:Article
{

 public new object AddOrUpdate(params object[] paras){

   //do your logic......

    return base.AddOrUpdate(paras);

 }


}

you mean this?this is inhret a class can do get or set BASE object property. 你的意思是这个?这是一个类可以做或获取或设置BASE对象属性。

if you do with delegate ,you must do more job with the same logic. 如果你使用委托,你必须用相同的逻辑做更多的工作。

but delegate can get the diffenent domain object and do more over-app thing. 但委托可以获得不同的域对象,并做更多的应用程序。

If GetA() is changed to a static method, you can simply call it within the GetB() function: 如果将GetA()更改为静态方法,则只需在GetB()函数中调用它:

class a { 
   public static void GetA() { 
     // some logic here 
   } 
} 

class b { 
    public void GetB() {
      a.GetA();
    }
} 

If GetA() is not static, it doesn't make sense since GetA() needs an object context (eg., the invisible "this" pointer) by definition. 如果GetA()不是静态的,那么它没有意义,因为GetA()需要一个对象上下文(例如,不可见的“this”指针)。 You can't pass an instance of object B to class A, as class A does not know anything about class B. 您不能将对象B的实例传递给A类,因为A类对B类一无所知。

What are you really attempting to do? 你真的想做什么?

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

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