I have a abstract class 'Building':
public abstract class Building {
abstract public int ID {get;}
abstract public string name {get;}
}
the class (for example) Headquarter : Building has the Variables for these getter and setter methods. The Problem is I have to write in every Subclass
private int _ID = 1;
public int ID {
get {return _ID;}
}
Is there a way to create for example one getter setter method like ahead, in the abstract class and save the code, so that I only have to set the variables? Thanks for helping.
Instead of making the properties abstract, you could make the setter protected, and/or allow them to be set in the constructor:
public abstract class Building
{
// Optional constructor
protected Building(int id, string name)
{
this.ID = id;
this.Name = name;
}
public int ID { get; protected set; }
public string Name { get; protected set; }
}
This moves the implementation into the base class, but still only allows the subclasses to set those values.