I have a class that I need to adapt to various scenarios. Some
properties and methods will be needed in every case, while other's are
unique for one case. So I made a base class, and a set of other derived
classes for each scenario.
But I don't understand this fully. There are not only methods in my
original class. It has data too, in private fields.
public class OriginalClass
{
private string field1;
private string field2;
// Constructor
public OriginalClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}
public void ProcessData()
{
// Using field1 and field2 here.
}
}
Does this mean it's no use having fields in an abstract class? I wonder
if I have to write derived classes so that each method takes all the
data necessary as parameters? Like this:
public class BaseClass
{
// No private fields and no constructor
public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}
public class DerivedClass
{
private string field1;
private string field2;
public DerivedClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
ProcessData(field1, field2);
}
}
Gustaf