-1

I hope you can help me with this c# question. I have some classes like:

public abstract class Animal
{
  public abstract void Walk(int param1,int param2);
}
public class Cat : Animal
{
  public override void Walk(int param1,int param2){}
}
public class SmallCat : Cat
{
  public override void Walk(int param1,int param2){}
}
public class Dog : Animal
{
  public override void Walk(int param1,int  param2){}
}

And I have a controller class for all animals.

someAnimal.Walk(a,b);

My question is : I dont want base class to know all details about moving but I need more parameters or different parameters for different animals on Walk function. What is good way to solve this?

By the way the changing parameters are only needed when the different user (controller) takes control of the animal.

After reading your comments I've decided to use a simple UserController class which has all user(changing parameters) details, and send this to Animal class when it takes control of it.

  • Not quite sure what you mean by `I dont want base class to know all details about moving` – Alex Jul 04 '17 at 13:18
  • 3
    Pass in something that contains the details you need? `abstract void Walk(IWalkDetails walkDetails)` – Alex K. Jul 04 '17 at 13:18
  • hmm thank you alex. I guess that's not posible to define a controller without it knowing all details. – özkan kum Jul 04 '17 at 13:23
  • 1
    So you're calling the Walk method once in the controller (Polymorphism) and you need to pass different number of parameters per case ? How can that be possible without down-casting (bad OOP practice usually)? – Zein Makki Jul 04 '17 at 13:23
  • how about **public abstract void Walk(params object[] arguments);** – Simple Fellow Jul 04 '17 at 13:42
  • how does the controller will know which parameter to send? – özkan kum Jul 04 '17 at 13:48

1 Answers1

-3

I would think that universal parameters for walking that may change each time the method is called, might be length and velocity. Any other parameters, specific to a concrete instance, could be provided to the instance at either the time of construction or through a mutator.

Joshua Jones
  • 1,364
  • 1
  • 9
  • 15
  • So you would need a new instance every single time you want to walk? That's pretty horrible – Camilo Terevinto Jul 04 '17 at 13:25
  • @CamiloTerevinto I really don't think you understood my answer. – Joshua Jones Jul 04 '17 at 13:29
  • actually this is a game I'm developing and animals can be controlled by different users but this may be a pretty solution. User controller can tell all the details when it takes control. Thanks. I dont know why this answer get many down votes – özkan kum Jul 04 '17 at 13:32
  • but when I think of it, that's also downcasting :) – özkan kum Jul 04 '17 at 13:41
  • @özkankum of course it's downcasting. The only way to do this is by making Walk receive an abstract class / interface – Camilo Terevinto Jul 04 '17 at 13:42
  • @CamiloTerevinto providing the Walk method with an interface as an argument will not solve the issue. It will only perpetuate the problem further down the call stack. The only solution that would work, using direct arguments to the Walk method, would be to provide a Map type as an argument, and I really dislike that approach because it eliminates the ability to error check arguments at compile time. – Joshua Jones Jul 04 '17 at 13:51