C#:从多个类扩展 [英] C#: Extending from more than 1 class
问题描述
假设我有一个ViewModel TabViewModel
扩展了 ObservableObject
MVVM Foundation Framework中ViewModels的类。然后,我还有一个扩展了 TabViewModel
的 EditorTabViewModel
,现在我需要扩展 DependencyObject
以实现DependencyProperties。我不能超过一堂课。我该如何实施?我可以有一个像这样的中间类……
Suppose I have a ViewModel TabViewModel
that Extends ObservableObject
the class for ViewModels in the MVVM Foundation Framework. Then I also have a EditorTabViewModel
that extends TabViewModel
, Now I need to extend DependencyObject
to implement DependencyProperties. I cannot extend more than 1 class. How might I implement this? I could have an "intermediate" class like ...
TabViewModel : ObservableObject
EditorTabViewModel : TabViewModel
DependentEditorTabViewModel : DependencyObject
但这就是1个多余的类。还有更好的方法吗?
but thats 1 extra unnecessary class. Any better way to do this?
更新
实际上我做不到的操作以上。 DependentEditorTabViewModel
仍然需要扩展EditorTabViewModel ...除了 DependencyObject
Ops actually I cant do the above. DependentEditorTabViewModel
still need to extend EditorTabViewModel ... apart from DependencyObject
推荐答案
C#不支持多重继承。最好的选择是使用接口,而不是父类。
C# does not support Multiple Inheritance. Your best bet is to use Interfaces, rather than parent classes.
即使您没有使用接口的选择(也许您无权使用代码),通常最好优先考虑组成而不是继承。您真的需要同时继承这两个类吗?还是可以与它们一起合成?
Even if you don't have the option of using interfaces (maybe you don't have access to the code), it's generally better to prefer composition over inheritance. Do you really need to inherit both of these classes, or can you compose with them instead?
这篇关于C#:从多个类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!