【问题标题】:vb .net override subclass method of inherited base classvb .net 覆盖继承基类的子类方法
【发布时间】:2015-03-16 12:26:17
【问题描述】:

我正在尝试为特定的 WPF 控件(Autodesk 的 Ribbontextbox - 基于 WPF 文本框)创建一个“原型”类,其中包括它自己的命令处理程序类。

RibbonTextBox 旨在将 ICommand 接口用于操作,而不是事件系统,这意味着它具有 Commandhandler 属性,我可以将命令分配给... 我的想法是创建一个基类,包括它自己的 Command-Class,所以在派生类中我只需要重写 execute 方法。

mustinherit class BaseClass
      inherits RibbonTextBox
sub new()    
    me.Commandhandler= New CommandBase
end sub 

public Class CommandBase
           implements ICommand
       Protected Overridable Function CanExecute(parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return Not parameter.value.ToString.IsEmptyStringOrNothing
    End Function

    Public Event CanExecuteChanged(sender As Object, e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Protected Overridable Sub Execute(parameter As Object) Implements System.Windows.Input.ICommand.Execute
'This Method should be defined in the derived Instance!!!
    End Sub

end class 

我想如何使用它:

public Class DerivedClass
            inherits BaseClass

public Overrides sub me.commandhandler.Execute() 'or some similar :-)
'here the definition of commandexecution
end sub 

end Class

感谢任何提示! 谢谢, D

【问题讨论】:

  • 我认为您必须使用事件/委托,您的 DerivedClass 必须处理 ExecuteEvent。另一种选择是拥有一个派生自 CommandBase 的 DerivedCommandClass,并将 DerivedClass 作为其构造函数的参数。
  • 我很害怕 :-( 我想避免一些事件处理开销....

标签: .net vb.net inheritance


【解决方案1】:

您可以通过这种方式从其他类覆盖。如果您不想使用事件,那么您可以使用其他接口来接收命令。

创建 CommandBase 时,您将类的实例作为参数传递。当 CommandBase 收到命令时,它会调用该实例。

MustInherit Class BaseClass
    Inherits RibbonTextBox
    Implements ICommandAction

    Sub New()
        Me.Commandhandler = New CommandBase(Me)
    End Sub

    Protected MustOverride Sub Execute(ByVal parameter As Object) Implements ICommandAction.Executecommand

End Class

Public Class CommandBase
    Implements ICommand

    Public Sub New(ByVal receiver As ICommandAction)
        _receiver = receiver
    End Sub

    Protected Overridable Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return Not parameter.value.ToString.IsEmptyStringOrNothing
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Protected Overridable Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _receiver.Executecommand(parameter)
    End Sub

End Class

Public Class DerivedClass
    Inherits BaseClass

    Protected Overrides Sub Execute(ByVal parameter As Object)

    End Sub

End Class

这是 ICommandAction 的样子

Interface ICommandAction

    Sub Executecommand(ByVal parameter As Object)

End Interface

【讨论】:

  • 我找不到 ICommandAction...我缺少什么...我监督的一些参考资料? BR,丹尼尔
  • @dba 这是一个自定义界面。我添加了它的外观示例。
【解决方案2】:

您必须使用OverridableOverrides 修饰符,例如here。此外,您需要确保基函数在继承的Class 中可见(PublicProtected)。最后,您需要确保使用正确的语法:

Public Overrides Sub Execute()
'here the definition of commandexecution
End Sub 

【讨论】:

  • 我想我正在使用可覆盖/覆盖修饰符,我的问题是,我不直接继承这个子类,我需要在我的派生类中覆盖一个嵌套实例方法......我不是派生命令库,而是派生基类,其中属性具有命令库的实例
  • 继承以传递的方式工作。如果一个类中有一个可重写函数,并且它是受保护的或公共的,那么即使在从上述类继承的类中继承的类中,您也可以重写它。
  • 我无法确认,嵌套的可重写函数/子可以在派生类中被重写。也许当我在直接父级(基础)中声明一个可覆盖的覆盖时......我想我会选择 the_lotus 的解决方案。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-11-20
  • 2012-05-08
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2014-01-15
相关资源
最近更新 更多