我有以下类:如何为我继承的类声明构造函数?
public abstract class TableServiceEntity
{
protected TableServiceEntity();
protected TableServiceEntity(string partitionKey, string rowKey);
.....
.....
}
我从该类继承如下:
public class AuditableTableServiceEntity : TableServiceEntity
{
public string CreatedBy { get; set; }
.....
.....
}
我使用类,如下所示:
public class Note : AuditableTableServiceEntity
{
public Note() { }
public Note(string dsValue, string pk)
: base(pk, Seq.GetNext(dsValue, pk, "Note").ToString("00000")) { }
失败说,类AuditableTableServiceEntity没有一个构造函数 需要两个参数。为了解决问题,我尝试将以下方法添加到AuditableTableServiceEntity中。这是一个有效的事情吗?还是有其他的方式?另外我注意到它需要我添加一个构造函数,它使用零参数。这是为什么?
public AuditableTableServiceEntity()
: base()
{
}
public AuditableTableServiceEntity(string pk, string rk)
: base(pk, rk)
{
}
有人可以给我建议,如果我在正确的轨道上,或者如果有更好的方法做到这一点。我是否应该将AuditableTableServiceEntity上的方法声明为protected,还应该将该类声明为abstract?
用户不应该能够直接构造AuditableTableServiceEntity的实例。那么我应该让构造函数受到保护,并且类抽象和属性是虚拟的吗?谢谢 –
@Melissa如果你不想让用户构造它,我会使构造函数受到保护,并且抽象类。这些属性是否应该是虚拟的是一个单独的问题... –