I have a C# class that looks like this:
public class Animal
{
public string _name = "Animal";
public string GetGreeting()
{
if (this._name == "Animal")
{
return "Hello friend!";
}
else
{
return $"Hello, '{this._name}'";
}
}
}
public class Tiger : Animal
{
// How do I set "_name" here?
}
I want to hard-code the _name
value in the Tiger
class. However, I don't want to force other classes to set the _name
value. How do I do that in C#?
CodePudding user response:
Just add a constructor:
public Tiger()
{
_name = "Tony"; // they're great!
}