简体   繁体   English

C# 仅在选定的派生类中使用基类方法

[英]C# use base class methods in selected derived classes only

I have common functions that should be used by certain derived classes only.我有一些只能由某些派生类使用的通用函数。 To code each function only once, I use a base class.为了只对每个函数编码一次,我使用了一个基类。 I need to define the functions as protected to hide them in the derived classes that do not use them, so there is no way of using virtual and override, cause the protection level can not be changed in this case.我需要将函数定义为protected 以将它们隐藏在不使用它们的派生类中,因此无法使用virtual 和override,导致在这种情况下无法更改保护级别。 Here is my code.这是我的代码。 That works the way I weant it to, but ios there no better way to do that ?这按照我想要的方式工作,但是 ios 没有更好的方法来做到这一点?

  class Base
  {
    protected void Initialize()
    {
    }

    protected void doTask1()
    {
    }

    protected void doTask2()
    {
    }
}

class Derived1 : Base
{
    public void Initialize()
    {
        base.Initialize();
    }

    public new void doTask1()
    {
        base.doTask1();  
    }
}

class Derived2 : Base
{
    public void Initialize()
    {
        base.Initialize();
    }

    public new void doTask2()
    {
        base.doTask2();
    }
}

Any help is welcome ...欢迎任何帮助......

doTask1() should only be available in class Derived1 and doTask2() only in class Derived2. doTask1() 应该只在 Derived1 类中可用,doTask2() 只在 Derived2 类中可用。 Thanks to the answer of @Fildor I solved it using interfaces.感谢@Fildor 的回答,我使用接口解决了这个问题。 All derived classes derive from the same base class.所有派生类都派生自同一个基类。 The base class implements all functions in the interfaces.基类实现接口中的所有功能。

public class Base : I_Derived1, I_Derived2 
{
  // implementations of all methods ...
  doTask1()  
  {  // ...   
  }
  doTask2()  
  {  // ...   
  }
  initialize()   
  {  // ...   
  }
{

public interface I_Derived1
{
  doTask1();
  initialize();
}
public interface I_Derived2
{
  doTask2();
  initialize();
}

class Derived1 : Base
{
}
class Derived2 : Base
{
}

// then the interfaces are used ...

I_Derived1 d1 = new Base();
I_Derived2 d2 = new Base();

d1.doTask1();
d2.initialize();
d2.doTask2();
// ...

This solved my problem, Thanks.这解决了我的问题,谢谢。 In my real project some of the class Base functions are used by 10 or more derived classes, some by all and some only by 2 or 3. The user gets a DLL which serves him Lists of instances of a derived class, each instance allowing him to use the functions tnhat make sense for it.在我的实际项目中,一些类 Base 函数被 10 个或更多派生类使用,一些被全部使用,有些只被 2 或 3 个使用。使用对它有意义的函数。

I think you would be better off with just Interfaces here:我认为在这里只使用接口会更好:

public interface IInit{
  void Initialize();
}

public interface I1: IInit {
   void DoTask1();
}

public interface I2: IInit {
   void DoTask2();
}

public class Impl: I1, I2
{
   public void Initialize() { /*...*/  }
   public void DoTask1(){ /*...*/  }
   public void DoTask2(){ /*...*/  }
}

usage:用法:

Impl obj = new Impl();
obj.Initialize();
obj.DoTask1(); // both
obj.DoTask2(); // visible

I1 obj1 = new Impl();
obj1.Initialize();
obj1.DoTask1();
// obj1.DoTask2(); < not visible

I2 obj2 = new Impl();
obj2.Initialize();
// obj2.DoTask1(); < not visible
obj2.DoTask2();

You could even take this one step further with explicit interface implementations .您甚至可以通过显式接口实现更进一步。

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

相关问题 将派生类的方法重构为公共基类的方法? (C#) - Refactoring Derived Classes' Methods to Common Base Class' Method? (C#) C#:定义基类中的方法实现和派生类中的属性 - C#: Define methods implementation in base class and properties in derived classes C#-需要基类上的接口,但仅派生类中的实现 - C# - require an interface on a base class but only the implementations in derived classes 在 C# 中的基类中使用泛型:如何确保基类中的方法返回派生类的类型? - Using Generics in Base Classes in C#: How to ensure methods in the base class return the derived class's type? C#:将扩展方法添加到基类,以便它们出现在派生类中 - C#: Adding extension methods to a base class so that they appear in derived classes 对派生类中的重写方法使用基类 Doxygen 注释 - Use base class Doxygen comment for overridden methods in derived classes 一个派生类的多个基类,在C#中继承 - Multiple base classes for one derived class, inheritance in C# 在派生类C#中引发基类事件 - Raise Base Class Events in Derived Classes C# C# - 让所有派生类都调用基类构造函数 - C# - Making all derived classes call the base class constructor 在派生类中定义基类构造函数方法 - Define base class constructor methods in derived classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM