相关疑难解决方法(0)

接口,抽象还是虚拟方法?

我有一堆系统,让我们打电话给他们A, B, C, D, E, F, G, H, I, J.

它们都有类似的方法和属性.有些包含完全相同的方法和属性,有些可能略有不同,有些可能会有很大差异.现在,我为每个系统都有很多重复的代码.例如,我有一个GetPropertyInformation()为每个系统定义的方法.我试图找出哪种方法是减少重复代码的最佳方法,或者下面的方法之一不是要走的路:

接口

public Interface ISystem
{
    public void GetPropertyInformation();
    //Other methods to implement
}

public class A : ISystem
{
    public void GetPropertyInformation()
    {
       //Code here
    }
}
Run Code Online (Sandbox Code Playgroud)

抽象

public abstract class System
{
    public virtual void GetPropertyInformation()
    {
        //Standard Code here
    }
}

public class B : System
{
   public override void GetPropertyInformation()
   {
      //B specific code here
    }
}
Run Code Online (Sandbox Code Playgroud)

超级基类中的虚方法

public class System
{ …
Run Code Online (Sandbox Code Playgroud)

c# oop abstract-class virtual-functions interface

8
推荐指数
1
解决办法
475
查看次数

标签 统计

abstract-class ×1

c# ×1

interface ×1

oop ×1

virtual-functions ×1