Now, the Character class is starting to behave more like a real object, but we can make this even better by adding a second constructor to take in a name at initialization and set it to the name field:
- Add another constructor to Character that takes in a string parameter, called name.
- Assign the parameter to the class's name variable using the this keyword. This is called constructor overloading:
public Character(string name)
{
this.name = name;
}
For convenience, constructors will often have parameters that share a name with a class variable. In these cases, use the this keyword to specify which variable belongs to the class. In the example here, this.name refers to the class's name variable, while name is the parameter; without the this keyword, the compiler will throw a warning because it won't be able to tell them apart.Â
- Create a new Character instance in LearningCurve...