I have base class Animal
, and I created many classes which inherits from it, such as Dog
, Cat
, Monkey
... now I want to implement the feature that based on user input, create the object based on the input so that I can run the override version functions inside them. Something like that:
//Animal.cs
abstract class Animal
{
protected abstract void Func()
}
//Dog(or other animal).cs
class Dog:Animal
{
protected override void Func() {Do something;}
}
//program.cs
public static void main()
{
Animal a = null;
string str = Console.ReadLine();
a = "new str()"; // should have some try-catch module, omit here
}
I have hundreds of animals, so using if
or switch
does not look a good solution; any good methods?