简体   繁体   中英

C# not running derived class function

I have a problem with a game I'm making. Basically I have a lot of fields, a basic type and other types. Logically I made derivatives of the basic field to add the extra functionality as needed.

However, it seems that when I call a function that is present in the base class, that function will be ran from the base class and not the derivative.

Here is some cut-down code so you can see what I mean.

public class Field
{
    public Field(Field exitN, Field exitE, Field exitS, Field exitW, bool barricadeAllowed, int returnTo, int xPos, int yPos)
    {
        //Actual logic is in the constructor, but unrelated to the below function
    }

    public void Function()
    {
        if (this is StartField)
        {
            Debug.WriteLine("StartField running base function!");
        }
        //Actual logic here. 
    }

}

.

class StartField : Field
{
    public StartField(Field exitN, Field exitE, Field exitS, Field exitW, string color, int xPos, int yPos)
        : base(exitN, exitE, exitS, exitW, false, 0, xPos, yPos)
    {
        //Again, constructor emptied due to unrelatedness. 
    }

    public new void Function()
    {
        Debug.WriteLine("StartField function");
        //Different logic then the base function
    }

Calling this funtion on a startfield object causes the "startfield running base function!" to appear in my debug bar, but not the "startfield function" This tells me only the base function is called even though the object knows it is of the type StartField.

If it makes any difference, all fields are saved through links with other fields. The variables ask for the type Field, but all are allowed in the variables (because inheritance) Can this in any way cause the call to go to the "Field" code rather then the "StartField" code (or code of any other non standard field, since this seems to be the case with all fields)

There are 2 workarounds I can think of The first workaround I can think of is checking the type and casting the field to it's actual type before running the function. But we're not allowed to use (class is Field) in the assignment. Plus it would require more code then needed for a simple function call.

The second workaround also uses the (class is Field) functionality, namely programming all field type functionality into the base class. Which not only uses (class is Field) but also just feels plain wrong (childs should have their own functionality, doesn't make sense if the base has it all. Might as well go with a single class and have the type be a variable instead of an inheriting member)

Basically, it sounds like you need Function to be a virtual method, which is overridden in StartField :

// In Field
public virtual void Function()
{
    if (this is StartField)
    {
        Debug.WriteLine("StartField running base function!");
    }
    //Actual logic here. 
}

// In StartField
public override void Function()
{
    Debug.WriteLine("StartField function");
    //Different logic then the base function
}

Your current code is not using polymorphism - it will always call Field.Function if the compile-time type of the expression you're calling Function on is just Field , because it's not virtual. Then in StartField you're explicitly saying, "I'm not trying to override the base method - I'm providing a new one."

See the MSDN page for the new modifier and the one on the virtual modifier for more details.

What if Function was made virtual ?

public virtual void Function()
{
    if (this is StartField)
    {
        Debug.WriteLine("StartField running base function!");
    }
    //Actual logic here. 
}

And then overridden in StartField , like this:

public override void Function()
{
    Debug.WriteLine("StartField function");
    //Different logic then the base function
}

Definitely don't try to work around this -- it's basic OOP.

You need to mark the base class method as virtual (or abstract, if it doesn't have its own logic) so that it can be overridden. Then mark the subclass method as an override. Check http://msdn.microsoft.com/en-us/library/ebca9ah3(v=vs.110).aspx for example.

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