简体   繁体   中英

c# inherited classes

I have a class called CombatMgr and inside of it I have I few different functions. When these functions get called I want the other classes that inherit CombatMgr to have the same function and it will be called within it.

    //Main class
    public class CombatMgr
    {
        public void EnterCombat()
        {
             //This gets called as CombatMgr.EnterCombat();
        }
    }

    //Side class
    public class RogueCombat : CombatMgr
    {
        public void EnterCombat()
        {
             //I want this function to be 
             //linked to the EnterCombat function from CombatMgr.
             //And called when the main function is called.
        }
    }

I need the code to work like this... When i call CombatMgr.EnterCombat() all child classes that are active needs to fire their EnterCombat() function. So almost like an Event OnCombat and all the listeners are the child classes that inherit.

//Side class
public class RogueCombat : CombatMgr
{
    public override void EnterCombat()
    {
         base.EnterCombat();
         //....
    }
}

You should use RogueCombat instance,not CombatMgr。

You need to make your base class method as virtual and override it in child class

//Main class
public class CombatMgr
{
    public virtual void EnterCombat()
    {
         //This gets called as CombatMgr.EnterCombat();
    }
}

//Side class
public class RogueCombat : CombatMgr
{
    public override void EnterCombat()
    {
         //I want this function to be 
         //linked to the EnterCombat function from CombatMgr.
         //And called when the main function is called.
    }
}

As you have said in the comment section of question that you want an event like behavior. Then you can do following

 //Main class
public class CombatMgr
{
    public void EnterCombat()
    {
        // write logic here you want to execute for every child instance.
        // then call the virtual protected method.
        this.OnEnterCombat();
    }

    protected virtual void OnEnterCombat() { }
}

//Side class
public class RogueCombat : CombatMgr
{
    protected override void OnEnterCombat()
    {
        // Write logic for child class here
    }
}

Also please not following

CombatMgr cb1 = new RogueCombat();
        CombatMgr cb2 = new RogueCombat();

        cb1.EnterCombat(); //  calling this will also call the OnEnterCombat() method for intance cb1 not for cb2

Maybe I'm mistaken, but I think what you want to do is the following, assuming that the "Main Function" is the one at the CombatMgr class, which makes sense to be a "Main Function", hence the name...

public class CombatMgr
{
    // Delegate and Event
    public delegate void Combat();
    public event Combat OnEnterCombat;

    // Your main function
    public void EnterCombat()
    {
        // Calling event, and all subscribers to it
        if (OnEnterCombat != null)
        {
            OnEnterCombat();
        }
    }
}

// Side Class 1
public class RogueCombat : CombatMgr
{
    public void EnterCombat()
    {
        Console.WriteLine("Rogue Combats");
    }
}

Then you would use it like this:

CombatMgr manager = new CombatMgr();

// Suscribing a Rogue combat
manager.OnEnterCombat += () => 
{
    RogueCombat rogue = new RogueCombat();
    rogue.EnterCombat();
};

manager.EnterCombat();

you should mark the function as virtual on the base class, and overriden on the inherited class:

//Main class
public class CombatMgr
{
    public virtual void EnterCombat()
    {
         //This gets called as CombatMgr.EnterCombat();
    }
}

//Side class
public class RogueCombat : CombatMgr
{
    public override void EnterCombat()
    {
         //I want this function to be 
         //linked to the EnterCombat function from CombatMgr.
         //And called when the main function is called.
    }
}

see more at: http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

I'm not really sure that I understood correctly what you are trying to achieve. Anyway, are you sure that RogueCombat should be derived from CombatMgr? What does RogueCombat inherit from CombatMgr? Just the names of the methods and the fact that they are called at the same time?

Remember that an instance of a class does not have any relationship at all with the instances of its derived classes, so you have to keep track of these instances inside your base classe, with a collection or an event.

Alfonso's code doesn't work because it doesn't work on the exixting instances of RogueCombat, but creates a new one every time EnterCombat is called in CombatMgr. To have it work you should register your derived classes' instances to the event in the base class as soon as they are created. You can do this in the constructor like this:

public class RogueCombat : CombatMgr
{
    public RogueCombat(CombatMgr aMgr)
    {
        aMgr.OnEnterCombat += new Combat(EnterCombat);
    }

    public void EnterCombat()
    {
        Console.WriteLine("Rogue Combats");
    }
}

For this to work you have to pass the instance of CombatMgr everytime you create an instance of RogueCombat. This wouldn't be necessary if you had a single instance of CombatMgr accessible from every point in your system (something like CombatMgr.Current).

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