接口方面,抽象的,或只是虚拟的方法呢? [英] Interface, Abstract, or just virtual methods?

查看:96
本文介绍了接口方面,抽象的,或只是虚拟的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bunch of systems, lets call them A, B, C, D, E, F, G, H, I, J.

它们都具有类似的方法和属性。有些包含相同的方法和属性,有的可能会略有不同,有的可能有很大的差异。现在,我对每个系统有很多重复的代码。例如,我有一个名为方法 GetPropertyInformation()是为每个系统定义。我试图找出哪些方法是减少重复代码或以下方法也许有最好的办法是不要去方式:

They all have similar methods and properties. Some contain the exact same method and properties, some may vary slightly and some may vary a lot. Right now, I have a lot of duplicated code for each system. For example, I have a method called GetPropertyInformation() that is defined for each system. I am trying to figure out which method would be the best approach to reduce duplicate code or maybe one of the methods below is not the way to go:

接口

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

public class A : ISystem
{
    public void GetPropertyInformation()
    {
       //Code here
    }
}

摘要

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

public class B : System
{
   public override void GetPropertyInformation()
   {
      //B specific code here
    }
}

<在超基类STRONG>虚拟方法

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

public class C : System
{
  public override void GetPropertyInformation()
  {
      //C Code
  }
}

有一个问题,尽管它可能是愚蠢的,是让我们假设我去了抽象的方法,我想重写 GetPropertyInformation ,但我需要它传递一个额外的参数,这是可能的,或者我会创造抽象类的另一种方法?例如, GetPropertyInformation(X)

One question, although it may be stupid, is let's assume I went with the abstract approach and I wanted to override the GetPropertyInformation, but I needed to pass it an extra parameter, is this possible or would I have to create another method in the abstract class? For example, GetPropertyInformation(x)

推荐答案

您抽象和超基类的方法都不太一样。你应该总是让基类抽象的,你可以提供一个默认的实现(虚方法)否(抽象方法)。决定因素是你是否曾经想拥有的基类的实例,我不这么认为。

Your abstract and 'super base class' approaches are not too different. You should always make the base class abstract, and you can provide a default implementation (virtual methods) or not (abstract methods). The deciding factor is whether you ever want to have instances of the base class, I think not.

所以它的基类和接口之间。如果你的A之间的强耦合,B C类那么你可以使用一个基类,可能是一个常见的实现。

So it's between base class and interface. If there is a strong coupling between your A, B C classes then you can use a base class and probably a common implementation.

如果在A,B,C类不自然属于一个家庭,然后使用一个接口。

If the A, B, C classes do not naturally belong to a single 'family' then use an interface.

系统不是这样的好名字。

And System is not such a good name.

和压倒一切,当你不能改变PARAMETERLIST。也许默认参数可以帮助,否则,你只需要2重载GetPropertyInformation()。

And you cannot change the parameterlist when overriding. Maybe default parameters can help, otherwise you just need 2 overloads for GetPropertyInformation().

这篇关于接口方面,抽象的,或只是虚拟的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆