简体   繁体   中英

How to set value for a base class from derived class in c# wpf

public class Baseclass
{
    private int mTotal = 0;
    private int mID = 0;

    public int Total
    {
        get { return mTotal; }
        set { mTotal = value;}
    }
    public int ID
    {
        get { return mID; }
        set { mID = value;}
    }
}

public class Derivedclass : Baseclass
{
    private int mX = 0;
    private int mY = 0;

    public int X
    {
        get { return mX; }
        set 
        {
            mX = value;
            Total = Total + mX;
        }
    }
    public int Y
    {
        get { return mY; }
        set 
        {
            mY = value;
            Total = Total + mY;
        }
    }

}

public partial class  Test : Page
{
    Baseclass B = new Baseclass();
    Derivedclass D = new Derivedclass();
    public Test()
    {
        Calculate();
    }     

    public void Calculate()
    {

        for(int i =1; i <5 ;i++)
        {
            B.ID = i;
            for (int j = 0; j < 5; j++)
            {
                D.X = j;
                D.Y = j;
            }
            MessageBox.Show("ID " + B.ID + " Total Sum" + B.Total);
            B.Total = 0;
        }
    }
    // Out put should be 
    // ID 1 Total Sum 16
    // ID 2 Total Sum 16
    // ID 3 Total Sum 16
    // ID 4 Total Sum 16
}

Here I have two classes, Baseclass and derivedclass. I want to find out the total sum of x and y property which is in derived class and set the total sum to Total property of base class And i want out should be like this

// ID 1 Total Sum 16
// ID 2 Total Sum 16
// ID 3 Total Sum 16

If you want to reference any inherited members, you can either just use the property name, as you have above. If there's a name conflict, or you'd like to be more clear, you can use the base keyword, like so:

base.Total = 15;

Here's the MSDN article on the base keyword: http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx

It seems like you're a little confused about the idea of inheritance, though. An instance of Baseclass isn't going to have any relationship to an instance of Derivedclass, or vice versa, because they're totally separate objects that lack references to one another. Inheritance controls the members - fields, properties, and methods - that a class has, and allows derived classes to reuse code from their parent while extending their functionality towards more specific use cases. In short, your B and D objects are never going to be talking to one another, and that's not really the point of inheritance in the first place.

So, without the useless B object, your Calculate method should look like so:

public void Calculate()
{
    for(int i = 1; i < 5; i++)
    {
        D.ID = i;
        for (int j = 0; j < 5; j++)
        {
            D.X = j;
            D.Y = j;
        }
        MessageBox.Show("ID " + D.ID + " Total Sum" + D.Total);
        D.Total = 0;
    }
}

This version of Calculate() will show the following:

ID 1 Total Sum 20
ID 2 Total Sum 20
ID 3 Total Sum 20
ID 4 Total Sum 20

You dont need to create separate instances for base and derived classes. Derived class will have base class properties in it.

Your Test class should look like this

public partial class  Test : Page
{
    Derivedclass D = new Derivedclass();
    public Test()
    {
        Calculate();
    }     

    public void Calculate()
    {

        for(int i =1; i <5 ;i++)
        {
            D.ID = i;
            for (int j = 0; j < 5; j++)
            {
                D.X = j;
                D.Y = j;
            }
            MessageBox.Show("ID " + D.ID + " Total Sum" + D.Total);
            D.Total = 0;
        }
    }
    // Out put should be 
    // ID 1 Total Sum 16
    // ID 2 Total Sum 16
    // ID 3 Total Sum 16
    // ID 4 Total Sum 16
}

your base class could have a virtual method. instead of a property

class Baseclass
{

public virtual int GetTotal(){return 0;} // or throw new NotImplementedException();
}


public class Derivedclass:Baseclass
{
    int x = 2;
    int y = 3;

    public override int GetTotal()
    {
        return x + y; 
    }
}

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