Refer to the "parent" of the class

Asked

Viewed 54 times

2

How do I reference the "father" within the method? I remember that in Java it was only used this, but in C#?

Example:

public class UPlayer : RocketPlayer
{

    public void SendMessage(object obj)
    {
        RocketPlayer pl = base;//não é possivel
    }

}

1 answer

3


base is used to call a method from one of the derived classes, ignoring methods from the current class (i.e., "non-virtual calls"). To reference the object itself whose code is being executed, use this, as in java.

RocketPlayer pl = this;

In my opinion, the question "How do I reference the "father" within the method?" does not make much sense. There are no two objects in this context (parent and child), there is only one. This object is of the type UPlayer, and that kind of drift RocketPlayer.

It is important to distinguish between types (classes, interfaces, etc.) and objects (instantiations of these types). In this context, there are 2 types but only 1 object that can be referenced.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.