简体   繁体   English

接口与继承

[英]interface & inheritance

C#, VS 2008 I have 4 cases say a,b,c,d, I plan to seperate them and create seperate classes the 4 cases have something in common, I put them in an interface and create a base class that implement the interface. C#,VS 2008我有4种情况说a,b,c,d,我打算将它们分开并创建单独的类这4种情况有共同点,我将它们放在接口中并创建实现该接口的基类。 now there are something in common between a&b, a&c, c&d, not sure how to make a good/clean implement 现在a&b,a&c,c&d之间有一些共同点,不确定如何制作好/干净的工具

thanks 谢谢

There are several options. 有几种选择。

You could have c and d inherit from a, and d inherit from c. 您可以让c和d从a继承,而d从c继承。 You could create a base class for each pair a/b, a/c, and c/d. 您可以为每对a / b,a / c和c / d创建一个基类。 You could duplicate functionality. 您可以复制功能。 You could provide the functionality via a helper class (static methods might be an option). 您可以通过助手类提供功能(可以选择静态方法)。

It really depends on what functionality is being shared, and the intended usage of the classes. 这实际上取决于要共享的功能以及类的预期用途。

It depends on how the common things works and how they relate to and use private/protected data, but often composition can be a complement or alternative to inheritance. 这取决于普通事物的工作方式以及它们如何与私有/受保护的数据相关联,以及如何使用私有/受保护的数据,但是合成通常可以作为继承的补充或替代。

Break out the common parts to helper classes that you use from the different implementations of a,b,c and d. 分解从a,b,c和d的不同实现中使用的帮助程序类的通用部分。

This is only possible if the implementation is not tightly coupled to the private data of each class. 仅当实现未紧密耦合到每个类的私有数据时,才有可能。

As a general rule, you should only use inheritance if your objects are different kinds of the same object. 通常,仅当对象是同一对象的不同种类时,才应使用继承。 If this is the case, then you can use inheritance to share implementation that's inherent in the definition of the base object. 如果是这种情况,那么您可以使用继承来共享基础对象定义中固有的实现。

If classes a,b,c and d aren't really different kinds of the same object then you can try encapsulating their common functionality in an internally referenced object. 如果类a,b,c和d实际上不是同一对象的不同种类,则可以尝试将它们的通用功能封装在内部引用的对象中。

public class a
{
    private CommonFunctionalityClass commonFunc;

    public a()
    {
        this.commonFunc = new CommonFunctionalityClass();
    }
} 

When you want to do the common stuff, you just call your instance of commonFunc. 当您想做普通的事情时,您只需调用commonFunc的实例即可。 You can do this same encapsulation for a/b, b/c, and c/d where you share functionality via a has a relationship using an internally referenced object. 您可以对a / b,b / c和c / d进行相同的封装,在其中您通过a使用内部引用的对象之间的关系共享功能。 That way you don't duplicate code, but you can share functionality flexibly. 这样,您就不会重复代码,但是可以灵活地共享功能。

public interface IABInterface
{
    //Whatever is common to A and B. It will have to be implemented in the classes
}

public interface IACInterface
{
    //Whatever is common to A and C. It will have to be implemented in the classes
}

public interface ICDInterface
{
    //Whatever is common to C and D. It will have to be implemented in the classes
}

public class ABCDBase
{
    //Whatever is common to all classes
}

public class A : ABCDBase, IABInterface, IACInterface
{
}

public class B : ABCDBase, IABInterface
{
}

public class C : ABCDBase, IACInterface, ICDInterface
{
}

public class D : ABCDBase, ICDInterface
{
}

You can create later in a static class extension methods for your interfaces to not duplicate the code for your methods in the Interfaces implementations (In other words, don't define methods in your interfaces, only properties). 您可以稍后在静态类扩展方法中为接口创建方法,以使其不重复接口实现中的方法代码(换句话说,不要在接口中定义方法,而仅定义属性)。 With refactoring can be really easy to implement the properties in your interfaces. 使用重构可以很容易地在您的接口中实现属性。 It would be nice to have extension properties. 具有扩展属性会很好。 Hopefuly in the future. 未来充满希望。

EDIT 编辑

Like this: 像这样:

public static class Helper
{
    public static void IABMethod1(this IABInterface aOrBObject, arguments args)
    {
        //This will be available for any A or B object without duplicating any code
    }
}

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

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