[英]Override property in derived class
I have asked a similar Question before but this time its a little extended. 我之前曾问过类似的问题 ,但是这次有所扩展。 What I had already asked was to override
SomeProperty
in the derived class for which the answer was using the 'new' operator. 我已经问过的是在派生类中重写
SomeProperty
,而答案使用的是“ new”运算符。
But now I am again in the same scenario but, 但是现在我又处于相同的情况下,
this time I have to execute a default behaviour in the base class while setting the
SomeProperty
in the derived class.这次,我必须在派生类中设置
SomeProperty
同时在基类中执行默认行为。
I have to force the user who is deriving from my base class to execute a set of code in base class while setting the SomeProperty
in the derived class. 我必须强迫从我的基类派生的用户在基类中执行一组代码,同时在派生类中设置
SomeProperty
。 I have already tried with Template Pattern but I need to bind this to a Custom Control in my Windows Phone Control Library. 我已经尝试过使用模板模式,但是需要将其绑定到Windows Phone控件库中的自定义控件。 So my base class cannot be a
abstract
class. 因此,我的基类不能是
abstract
类。 So I cannot specify a common functionality in the base class and make sure that the functionality is enforced while the variable is set in the derived class. 因此,我无法在基类中指定通用功能,并且不能确保在派生类中设置变量时强制执行该功能。
Is there any other way I could enforce this code (or kind of rule) executes in the base class while setting the property in the derived class? 在派生类中设置属性时,还有其他方法可以强制在基类中执行此代码(或某种规则)吗?
Edit: I should also mention that the person who is deriving from the base class is not expected to call the specific behaviour in base class. 编辑:我还应该提到,从基类派生的人不应调用基类中的特定行为。 I need to force the invocation.
我需要强制调用。
+---------------+
| UIElement |
|---------------| +----------------------+
| ... | | My Windows Application
| SomePropert{} | |----------------------|
| //Force to |<---+ |+--------------------+|
| //to xcute this code ||MyUserControl ||
+---------------+ | ||--------------------||
+--------------+-----+ || ||
|FrameWorkElement | |+--------------------+|
|--------------------| |//Want to use |
| ... |<-+ |// SomeProperty; |
+--------------------+ | | |
+-----------+-+ | |
|Control | | |
|-------------| +----------------------+
| ... |<---+
+-------------+ |
+-----------+---+
| UserControl |
|---------------|<---+
| ... | |
+---------------+ |
+----------+-------+
| MyUserControl |
|------------------|
| SomeProperty{} |
| //Want to override
| //Setting this here|
|//should make sure|
|//base class code |
|//gets executed |
+------------------+
If I'm understanding your question correctly, you can use the base
keyword to ensure that the original method is executed as well. 如果我正确理解了您的问题,则可以使用
base
关键字来确保也执行原始方法。 For example, 例如,
public new void Method()
{
DoMyCustomStuff();
base.Method();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.