C#使用密封类的多重继承 [英] C# Multiple inheritance using sealed class

查看:144
本文介绍了C#使用密封类的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI all



我创建了一个使用图搜索的代码库,并希望为图中的每个节点显示一个标签。



标签控制代码由Microsoft创建,因此它是一个密封单元。我想要这样,当用户点击标签时,更新与标签关联的节点对象。



类似于:



HI all

I have created a library of code that uses graph searching, and would like to display a label for each node in the graph.

The label control code was created by Microsoft, so it is a sealed unit. I would like the have it such that when a user clicks on the label, the node object associated with the label is updated.

Something like:

class AStarGridSearchLabel: System.Windows.Forms.Label, AStarGridSearchItem
{
}





AStarGridSearchItem继承自AStarSearchElement。



我的基本方案是A *搜索,其中我有AStarSearchElements和AStarSearchAgent。

然后我创建了一个包含AStarGridSearchItem的AStarSearchGrid。

现在我想在表单上显示它,而不必重写大量无关的代码。



AStarGridSearchItem是一个具体的类,System.Windows.Forms.Label也是如此。



Any关于如何实现这个目标的想法?



谢谢

MT



AStarGridSearchItem inherits from the AStarSearchElement.

The basic scheme I have is an A* search where I have AStarSearchElements and an AStarSearchAgent.
I then created a AStarSearchGrid that contains AStarGridSearchItem.
Now I want to display this on a form without having to rewrite lots of extraneous code.

AStarGridSearchItem is a concrete class and so is the System.Windows.Forms.Label.

Any ideas on how I can achieve this?

Thanks
MT

推荐答案

C#不支持多重继承



为什么C#不支持多重继承? [ ^ ]



您可以做的是在 AStarGridSearchLabel 中包装 AStarGridSearchItem 类并实现一个接口,例如



Multiple inheritance is not supported by C#

Why doesn't C# support multiple inheritance?[^]

What you could do is wrap the AStarGridSearchItem class in AStarGridSearchLabel and implement an interface, e.g.

interface IStarGridSearchItem
{
    void Foo();
}

class AStarGridSearchItem
{
    public void Foo() { /* ... */ }
}

class AStarGridSearchLabel : Label, IStarGridSearchItem
{
    private AStarGridSearchItem _item = new AStarGridSearchItem() ;

    // Implement the interface, and wrap AStarGridSearchItem
    public void Foo()
    {
        _item.Foo();
    }
}


这篇关于C#使用密封类的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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