[英]C#: Extending from more than 1 class
Suppose I have a ViewModel TabViewModel
that Extends ObservableObject
the class for ViewModels in the MVVM Foundation Framework. 假设我有一个ViewModel
TabViewModel
,它将ObservableObject
扩展为MVVM Foundation Framework中ViewModels的类。 Then I also have a EditorTabViewModel
that extends TabViewModel
, Now I need to extend DependencyObject
to implement DependencyProperties. 然后我也有一个
EditorTabViewModel
延伸TabViewModel
,现在我需要延长DependencyObject
实现DependencyProperties。 I cannot extend more than 1 class. 我不能超过1班。 How might I implement this?
我该如何实现呢? I could have an "intermediate" class like ...
我可以有一个“中间”类,如......
TabViewModel : ObservableObject
EditorTabViewModel : TabViewModel
DependentEditorTabViewModel : DependencyObject
but thats 1 extra unnecessary class. 但那是一个额外的不必要的课程。 Any better way to do this?
有更好的方法吗?
UPDATE UPDATE
Ops actually I cant do the above. Ops实际上我无法做到以上。
DependentEditorTabViewModel
still need to extend EditorTabViewModel ... apart from DependencyObject
DependentEditorTabViewModel
仍然需要扩展EditorTabViewModel ...除了DependencyObject
C# does not support Multiple Inheritance . C#不支持多重继承 。 Your best bet is to use Interfaces, rather than parent classes.
最好的办法是使用Interfaces,而不是父类。
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?
你真的需要继承这两个类的,或者你可以用它们组成呢?
Its not an extra class if you're accomplishing what you need. 如果你正在完成你需要的东西,它不是一个额外的课程。 Here is how you would go about that:
以下是你将如何做到这一点:
DependentEditorTabViewModel : DependencyObject
TabViewModel : DependentEditorTabViewModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.