Jumper404
Jumper404

Reputation: 27

Polymorphism, Calling child method of parent

Hi everyone I am programming in Unity3d with C# and while I was writing my code I stumbled with a little issue, I write to you an example because I dont know explain me.

class Base
{
    public string name;
}
class Derived : Base
{
    public void Gun();
}

class BasePlayer
{
    public Base x;
}

class SoldierPlayer : BasePlayer
{ 

}

The situation is this, I want to do something like that

SoldierPlayer.x.Gun();

But I don't know how do it

The real case is this

 public class BasePlayerController : MonoBehaviour
        {

            public BasePlayerManager playerManager; 
...
public class RobotPlayerController : BasePlayerController {
...
playerManager = gameObject.AddComponent<RobotPlayerManager>();

And I will use new methods

UPDATE 1

I did a example better, I want to do in Base Controller manager.user.energy and be treated as the next type RobotManager.RobotUser.energy

BaseController
BaseManager
BaseUser

class BaseController
{
    BaseManager manager;
    public virtual void Move(int x,int y)...
}

class BaseManager {
    BaseUser user;
    public virtual Pause(bool state);
}

class BaseUser {
    int life
}

RobotController
RobotManager
RobotUser

class RobotController : BaseController
{
    // manager as RobotManager?
    public void Ray(int x,int y);
}

class RobotManager : BaseManager
{
    // user as RobotUser?
}

class RobotUser : BaseUser 
{
    int energy;
}

UPDATE 2 I seek to do this

public Run()
{
    RobotController rc = new RobotController();
    rc.manager.energy;
}

Upvotes: 2

Views: 1531

Answers (1)

Maxim Kitsenko
Maxim Kitsenko

Reputation: 2343

You can't call SoldierPlayer.x.Gun(); because SoldierPlayer.x has type Base which has not method Gun(). OOP world and C# can provide you many solutions, your choose depends on your goals.

some of them (order by best practise):

1) Overriding Polymorphism. Add .Gun() method to Base class and implemend it in derived classes. For example

class Base
{
    public string name;
    public void virtual Gun()
    {
        Trace.Log("I'm base class, i can't do anything");
    }
}
class Derived : Base
{
    public override void Gun()
    {
        Consule.WriteLine("Hello i have gun");
    }
}

class Derived2 : Base
{
    public override void Gun()
    {
        Consule.WriteLine("Hello i have 2 guns");
    }
}

2) Overloading Polymorphism In many source this method is mentioned like some kind of polymorphism AD-HOC

public void GunAction(Derived2 o)
{
    o.Gun();
}
public void GunAction(Derived1 o)
{
    o.Gun();
}
public void GunAction(Base o)
{
    Trace.Log("I'm base class, i can't do anything");
}

3) is-cast

public void GunAction(Base o)
{
    if(o is Derived1 )
        o.Gun();
    if(o is Derived2 )
        o.Gun();
}

UPDATE 1 answering to your new requirements

class BaseController
{
    public BaseManager manager;
...

class RobotController1 : BaseController
{
    // manager as RobotManager? - no it is stil BaseManager
    public void Ray(int x,int y);
}


class RobotController2 : BaseController
{
    // manager as RobotManager? - yes. now it is RobotManager 
    public void Ray(int x,int y);

    public RobotController2()
    {
        manager = new RobotManager();
    }
}

public void Run()
{
    var controller = new RobotController2();// you have RobotManager 
    controller.manager = new BaseManager();// it is again BaseManager
}

Upvotes: 2

Related Questions