如何只写一次childs的构造函数的一部分?

How to write a part of the constructors of childs only once?

我目前正在使用抽象的 class 房间,其中有七个不同的 child classes。如果我向抽象 class 添加一些内容(比如名称 属性 ).

添加必须为每个 child 设置 same/instantiated 的 属性 时,我以何种方式不违反 DRY 原则?

示例:

public abstract Room
{
    // Need to be assigned in constructor.
    protected int RoomNumber { get; set; } 
    protected int PositionX { get; set; }
    protected int PositionY { get; set; }

    // Always the same at the start
    protected List<Guest> GuestsInRoom { get; set; }
    protected string ImageFilePath { get; set; }
}

public class Bedroom : Room
{
    private string Classification { get; set; }
    public Bedroom()
    {
        // Assign/instantiate all properties.
    }
}

public class Bathroom : Room
{
     private string SomeOtherProperty { get; set; }
     public Bedroom()
     {
         // Assign/instantiate all properties again
     }
 }

向基础添加一个构造函数class。如果有必须分配的字段,请将它们设为必填参数。您可以使用“:base()”调用非空父 class 构造函数,如修改后的卧室 class.

所示
public abstract Room
{
    // Need to be assigned in constructor.
    protected int RoomNumber { get; set; } 
    protected int PositionX { get; set; }
    protected int PositionY { get; set; }

    // Always the same at the start
    protected List<Guest> GuestsInRoom { get; set; }
    protected string ImageFilePath { get; set; }

    protected Room(int roomNumber, int positionX, int positionY)
    {
        RoomNumber = roomNumber;
        PositionX = positionX;
        PositionY = positionY;
        GuestsInRoom = new List<Guest>();
    }
}

public class Bedroom : Room
{
    private string Classification { get; set; }
    public Bedroom(string classification, int roomNumber, int positionX, int positionY) 
        : base(roomNumber, positionX, positionY)
    {
        // Assign/instantiate all properties.
        Classification = classification;
    }
}