-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.

6
  • Not quite sure what you mean by I dont want base class to know all details about moving Commented Jul 4, 2017 at 13:18
  • 3
    Pass in something that contains the details you need? abstract void Walk(IWalkDetails walkDetails) Commented Jul 4, 2017 at 13:18
  • hmm thank you alex. I guess that's not posible to define a controller without it knowing all details. Commented Jul 4, 2017 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)? Commented Jul 4, 2017 at 13:23
  • how about public abstract void Walk(params object[] arguments); Commented Jul 4, 2017 at 13:42

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

6 Comments

So you would need a new instance every single time you want to walk? That's pretty horrible
@CamiloTerevinto I really don't think you understood my answer.
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
but when I think of it, that's also downcasting :)
@özkankum of course it's downcasting. The only way to do this is by making Walk receive an abstract class / interface
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.