c# – 继承正确

我有课:

internal class Stage
{
    public long StageId { get; set; }
    public string StageName { get; set; }
    public int? Order { get; set; }

    public Stage()
    {
        Order = 0;
    }
}

我也有:

public class GroupStage : Stage
{
    private override long StageId { set { StageId = value; } }

    public GroupStage() : base() { }

    public void InsertStage(long groupId)
    {
    }

    public static void SetStageOrder(long stageId, int order)
    {
     ....
    }

    public static void DeleteStage(long stageId)
    {
     ....
    }

    public static GroupStage[] GetStages(long groupId)
    {
     ....
    }
}

和:

public class TaskStage : Stage
{
    public DateTime? Time { get; set; }

    public TaskStage()
        : base()
    {
     ....
    }

    public static Stage GetNextTaskStage(Guid companyId, long taskId)
    {
     ....
    }

    public static Stage[] GetTaskStages(Guid companyId, long taskId)
    {
     ....
    }
}

这不起作用,我得到例外:
可访问性不一致:基类Stage比GroupStage类更难访问

我希望Stage类是私有的,除了GroupStage和TaskStage之外没有访问权限.我还想让StageId在GroupStage和TaskStage中成为私有的.
如何在不重复GroupStage和TaskStage中的Stage成员的情况下执行此操作?

您不能使派生类比它的基类更易于访问.您可以做的是使TaskStage和GroupStage内部,然后继承和公开公共接口,以便只有接口在程序集外可见.

public interface IGroupStage
{
    public string StageName{ get; set; }
    ...
}

interal class GroupStage : IGroupStage
{
...
}
相关文章
相关标签/搜索