什么是多继承一些很好的替代品。NET? [英] What are some good alternatives to multiple-inheritance in .NET?

查看:254
本文介绍了什么是多继承一些很好的替代品。NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了一下我的类层次结构的问题,在WPF应用程序。它是那些你有两个继承树合并在一起的问题之一,你找不到任何逻辑的方式,使你的产业工作顺利没有的多重继承。我想知道如果任何人有什么好主意,为获得这种系统的工作,没有使其无法跟随或调试。

I've run into a bit of a problem with my class hierarchy, in a WPF application. It's one of those issues where you have two inheritance trees merging together, and you can't find any logical way to make your inheritance work smoothly without multiple inheritance. I'm wondering if anyone has any bright ideas for getting this kind of system working, without making it impossible to follow or debug.

我是一个低级的工程师,所以我第一个想到的总是:哦!我只是写一些这些类的本地C ++,并引用它们的外部!然后,我可以有我所有的旧学校面向对象有趣!唉,这不利于当你需要从托管控件继承...

I'm a low-level engineer, so my first thought is always, "Oh! I'll just write some of these classes in native C++, and reference them externally! Then I can have all my old-school OO fun!" Alas, this doesn't help when you need to inherit from managed controls...

让我显示我目前预计的类图的一个片段:

Allow me to show a snippet of my current projected class diagram:

 ____________________________________      _____________________________________
| CustomizableObject                 |    | System.Windows.Controls.UserControl |
|____________________________________|    |_____________________________________|
|   string XAMLHeader()              |                        ▲
|   string XAMLFooter()              |◄--┐                    |
|   CustomizableObject LoadObject()  |   \                    |
|   <Possible other implementations> |    \                   |
|____________________________________|     \                  |
         ▲                      ▲           \                 |
         |                      |            \                |
         |                      |             \               |
 _________________    ______________________   \    _____________________
| SpriteAnimation |  | SpriteAnimationFrame |  └---| CustomizableControl |
|_________________|  |______________________|      |_____________________|
                                                      ▲             ▲
                                                      |             |
                                                      |             |
                                                  ________    _____________
                                                 | Sprite |  | SpriteFrame |
                                                 |________|  |_____________|

现在的问题是pretty的明确:CustomizableObject和CustomizableControl对象树的树分离---和用户控件插入到一个,但不能同时使用。

The problem is pretty clear: the separation of the CustomizableObject and CustomizableControl object trees --- and the insertion of UserControl into one, but not both, of the trees.

这是没有实际意义动CustomizableObject执行到它的派生类,因为实现不按类而异。此外,这将是非常混乱,有它实施多次。所以,我真的不想让Cu​​stomizableObject的接口。该接口的解决方案没有任何意义,我。 (接口有从来没有真的取得了很大的意义,我,说实话......)

It makes no practical sense to move the implementation of CustomizableObject into its derived classes, since the implementation doesn't vary by class. Furthermore, it would be really confusing to have it implemented multiple times. So I really don't want to make CustomizableObject an interface. The interface solution doesn't make any sense to me. (Interfaces have never really made much sense to me, to be honest...)

所以,我再说一次,任何人有什么好主意?这一次是一个真正的泡菜。我想详细了解使接口与我的对象树,而不是反对。我正在使用WPF和C#作为一个坚实的运动,比什么这个简单的精灵引擎。这是很容易解决C ++ - 但我需要找出如何解决这些问题在托管环境中,而不是抛出我的手在空中跑Win32的,只要吃不了艰难

So I say again, anyone have any bright ideas? This one's a real pickle. I'd like to learn more about making interfaces work WITH my object tree, rather than against it. I'm making this simple sprite engine using WPF and C# as a solid exercise, more than anything. This is so easy to solve in C++ - but I need to figure out how to solve these problems in a managed environment, rather than throwing my hands in the air and running back to Win32 whenever the going gets tough.

推荐答案

一种方法是使用扩展方法与接口,提供你的派生类的实现,就像System.Linq.Queryable:

One approach is to use extension methods with an interface to provide your "derived class" implementation, much like System.Linq.Queryable:

interface ICustomizableObject
{
    string SomeProperty { get; }
}

public static class CustomizableObject
{
    public static string GetXamlHeader(this ICustomizableObject obj)
    {
        return DoSomethingWith(obj.SomeProperty);
    }

    // etc
}

public class CustomizableControl : System.Windows.Controls.UserControl, ICustomizableObject
{
    public string SomeProperty { get { return "Whatever"; } }
}

用法:只要你有一个using指令(或在同一个命名空间),你的扩展方法定义的命名空间:

Usage: As long as you have a using directive for (or are in the same namespace as) the namespace where your extension methods are defined:

var cc = new CustomizableControl();
var header = cc.GetXamlHeader();

这篇关于什么是多继承一些很好的替代品。NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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