C#:从多个类扩展
假设我有一个 ViewModel TabViewModel
,它扩展了 MVVM 基础框架中 ViewModel 的 ObservableObject
类。然后我还有一个扩展 TabViewModel
的 EditorTabViewModel
,现在我需要扩展 DependencyObject
来实现 DependencyProperties。我不能延长超过一堂课。我该如何实现这个?我可以上一门“中级”课程,比如……
TabViewModel : ObservableObject
EditorTabViewModel : TabViewModel
DependentEditorTabViewModel : DependencyObject
但那是一门额外的不必要的课程。有更好的方法来做到这一点吗?
更新
实际上我无法执行上述操作。 DependentEditorTabViewModel
仍然需要扩展 EditorTabViewModel ...除了 DependencyObject
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
but thats 1 extra unnecessary class. Any better way to do this?
UPDATE
Ops actually I cant do the above. DependentEditorTabViewModel
still need to extend EditorTabViewModel ... apart from DependencyObject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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?
如果您完成了所需的任务,那么这不是一个额外的课程。您将按照以下方式进行操作:
Its not an extra class if you're accomplishing what you need. Here is how you would go about that: