Override a property defined in base class

2019-06-15 01:16发布

问题:

I have case where the class hierarchy is something like this,

   +---------------+
   | UIElement     |
   |---------------|                            +----------------------+
   | ...           |                            | My Windows Application
   | SomePropert{} |                            |----------------------|
   |               |<---+                       |+--------------------+|
   |               |    |                       ||MyUserControl       ||
   +---------------+    |                       ||--------------------||
         +--------------+-----+                 ||                    ||
         |FrameWorkElement    |                 |+--------------------+|
         |--------------------|                 |//Want to use         |
         |    ...             |<-+              |// SomeProperty;      |
         +--------------------+  |              |                      |
                     +-----------+-+            |                      |
                     |Control      |            |                      |
                     |-------------|            +----------------------+
                     |  ...        |<---+
                     +-------------+    |
                            +-----------+---+
                            | UserControl   |
                            |---------------|<---+
                            |  ...          |    |
                            +---------------+    |
                                      +----------+-------+
                                      | MyUserControl    |
                                      |------------------|
                                      | SomeProperty{}   |
                                      | //Want to override
                                      |                  |
                                      +------------------+

Now in my app (and all other apps where I can export this MyUserControl) can I set the SomeProperty that is handled by the MyUserControl class rather than UIElement?

I am right now doing this by creating an object to MyUserControl and assigning that to the control that I added in my xaml page.So right now looks like this,

MyUserControl newControl = new MyUserControl();
web = windowsPhoneControl11;  //windowsPhoneControll1 is the 
                              //one that I added from toolbox.
                              // i.e., mycustomecontrol added from toolbox.

So now since I am using the 'new' it gets overriden. But when I export this control I can't expect the user to create a new object and assign it to the control that one is using in the xaml page.

So is there any other way I could override this one property so that the assignment of that property is handled by MyUserControl class rather than the UIElement class? What I mean about MyUserControl having the control to set this property is that I need to check for some value before assigning it. If it is not atleast an expected value then I need to set it to a default value.

Ps: I am sorry for such a long question but I couldn't express it more precise and I was not able to find anyother question related to this. And it is WindowsPhoneApp... Not the ordinary windowsapplication.

回答1:

Forgive me if I've interpreted this incorrectly but would the following work:

public class BaseClass
{
    public int MyProperty
    {
       get; set;
    }
}
public class ChildClass : BaseClass
{
    public new int MyProperty
    {
       get
       {
           return base.MyProperty;
       }
       set
       {
           if(DoYourCheckingStuff(value))
           {
               base.MyProperty = value;
           }
       }
    }
}

Didn't test this.

Although this feels like a really hack-ish way of doing it. What property are you actually trying to 'have control' over? Since there may be easier ways of doing this.

An example: Change a UserControl so that it's width can't be set between 100 and 200 (Although this is probably a pretty bad way to do it), by hiding it's Width property:

public class MyUserControl : UserControl
{
    public new double Width
    {
        get
        {
            return base.Width;
        }
        set
        {
             if(!(value > 100 && value < 200))
                 base.Width = value;
        }
    }
}


回答2:

you can override properties just like methods.

public new int Age()
{
    get
    {
        return Age ?? 21;
    }
    set
    {
        Age = value;
    }
}