简体   繁体   中英

how to get value of property from the base class?

i need to get the value of the property which is in the base class of the instance of the object, without setting the child classes property to retrieve it from the parent, possible?

在此处输入图片说明

I think it's possible you're misunderstanding inheritance here. Allow me to explain a little. Apologies in advance if you know this already - if that's the case then your question could probably use some clarification because this is what it sounds like you're asking. If you've got a derived class that inherits from some base class, then that derived class has all of the same properties that the base class has. For example:

public class Square
{
    public int xSize { get; set; }
}

public class Rectangle : Square
{
    public int ySize { get; set; }
}

If you declare Rectangle rect = new Rectangle() , you can access rect.xSize and rect.ySize just the same. The only time you wouldn't be able to do this is if xSize was private. Just because it got xSize from the Square class doesn't mean it has to go and get it from the parent class; it has its own. There's a reason the terminology here uses words like "parent" and "inherit" - if someone says you have your father's eyes, it doesn't mean you're literally using his eyes; it means they're the same because you inherited them from him. It's the same here - a derived class doesn't borrow properties from the parent class; it just has all of the properties that the base class would have, plus whatever extra ones you've given it.

Lets say you have a two classes that look like this:

public class Parent 
{ 
  public int Age { get; set; }
}

public class Child : Parent 
{
  // This will get the 'base' class property
  public int AgePlusFive { get { return base.Age + 5; } }
}

... later some place else you want to get the Age property from the Child class so you say:

Child child = new Child();
int age = child.Age;
int agePlusFive = child.AgePlusFive;

I know this is old, but you can do the following. If I know other classes will depend on common values say accounting data from an important class, I just make the property static and then it doesn't matter where you call it from (see below). Otherwise, just pass the values you want to share into the child Constructor.

public class Parent
{
    public static int Age { get; set; }
    public void SetAge(int age)
    {
        Age = age;
    }
}

public class Child : Parent
{
    public int AgePlusFive { get { return Parent.Age + 5; } }
    public void ShowProperty()
    {
        MessageBox.Show(AgePlusFive.ToString());
    }
}
}

Then you can call it like so from your static void main:

 Parent parent = new Parent();
 parent.SetAge(3);
 Child child = new Child();
 child.ShowProperty();

Note: You can still use base.Age for your child AgePlusFive get, but you'll have to add a method to the Parent that returns the Age.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM