I have a C# abstract class:
public abstract class BaseScript : MonoBehaviour
{
public float numberOne = 5f;
public float numberTwo = 10f;
public float result;
public void NumbersAdd()
{
result = numberOne + numberTwo;
}
public void NumbersMultiply()
{
result = numberOne * numberTwo;
}
}
What I would like to do is have another class that inherits from this, but only partially, so that it only inherits the public float result variable and the NumbersAdd() method.
But currently when I create that class with public class InheritingScript : BaseScript, it causes it to inherit all methods and variables.
Is there a way to avoid this? I especially want to avoid grabbing all variables, as in my actual project this is hundreds of variables, and it is very clutter-y.
I tried simply creating an instance of the other class to communicate with it, but I am pretty sure I need it in a base/inheritance structure for my project's purpose.
Pretty simple stuff. If you inherit a class, you inherit all its members. If you want a derived class to only inherit some members of a base class then there must be some other base class from which only those members are inherited. Your own derived class would inherit the base class with only the members you want to inherit, e.g.
If only
ClassBwere declared and had both member fields,ClassCinheritingClassBwould mean inheriting both fields. By splitting out the members into two classes with one inheriting the other, you can get all members by inheritingClassBor just some members by inheritingClassA. You can do this through as many layers of inheritance as you like.Consider how controls work in WinForms. The
Panelclass inheritsScrollableControl, which inheritsControl, which inheritsComponent. Other classes may inheritScrollableControlwhile others might inheritControland others might inheritComnponent, depending on what functionality they need to inherit. This is what makes an inheritance hierarchy a hierarchy.