【发布时间】:2011-09-28 20:24:30
【问题描述】:
我有一堆系统,我们称它们为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
}
}
摘要
public abstract class System
{
public virtual void GetPropertyInformation()
{
//Standard Code here
}
}
public class B : System
{
public override void GetPropertyInformation()
{
//B specific code here
}
}
超级基类中的虚拟方法
public class System
{
public virtual void GetPropertyInformation()
{
//System Code
}
}
public class C : System
{
public override void GetPropertyInformation()
{
//C Code
}
}
一个问题,虽然它可能很愚蠢,但让我们假设我采用抽象方法并且我想覆盖GetPropertyInformation,但我需要传递一个额外的参数,这是可能的还是我必须创建抽象类中的另一个方法?例如GetPropertyInformation(x)
【问题讨论】:
标签: c# oop interface abstract-class virtual-functions