C# 基类重载
我有一个应用程序实体的基类:C# 基类重载,c#,C#,我有一个应用程序实体的基类: public class LadderEntityBase : ICloneable { public Guid PK { get; set; } public string Name { get; set; } public string Comment { get; set; } public object Clone() { return this.MemberwiseClone(); } }
public class LadderEntityBase : ICloneable
{
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
然后从它派生出两个类,具有相同的功能重载,这允许创建带有或不带PK(Guid)参数的类。如果省略PK参数-将创建新Guid:
public class Order : LadderEntityBase
{
public Order() : this(Guid.NewGuid())
{
}
public Order(Guid guid)
{
this.PK = guid;
}
public string OrderFrom { get; set; }
}
及
是否可以将Order和Parcel的两个构造函数都移动到基类中?我相信这是您所能做到的。移动基类中的所有逻辑,只保留继承的基类调用
public class LadderEntityBase : ICloneable
{
public LadderEntityBase(Guid pk)
{
PK = pk;
}
public LadderEntityBase() : this(Guid.NewGuid())
{
}
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
public class Order : LadderEntityBase
{
public Order() : base()
{
}
public Order(Guid guid) : base(guid)
{
}
public string OrderFrom { get; set; }
}
public class Parcel : LadderEntityBase
{
public Parcel() : base()
{
}
public Parcel(Guid guid) : base(guid)
{
}
public string SentTo { get; set; }
}
我相信这是你能走的最远的地方了。移动基类中的所有逻辑,只保留继承的基类调用
public class LadderEntityBase : ICloneable
{
public LadderEntityBase(Guid pk)
{
PK = pk;
}
public LadderEntityBase() : this(Guid.NewGuid())
{
}
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
public class Order : LadderEntityBase
{
public Order() : base()
{
}
public Order(Guid guid) : base(guid)
{
}
public string OrderFrom { get; set; }
}
public class Parcel : LadderEntityBase
{
public Parcel() : base()
{
}
public Parcel(Guid guid) : base(guid)
{
}
public string SentTo { get; set; }
}
您可以在基类本身上设置PK,然后使用Base关键字设置GUID(如果您希望两个派生类使用不同的GUID)
您可以在基类本身上设置PK,然后使用Base关键字设置GUID(如果您希望两个派生类使用不同的GUID)
public class LadderEntityBase : ICloneable
{
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public LadderEntityBase(Guid guid)
{
this.PK = guid;
}
public object Clone()
{
return this.MemberwiseClone();
}
}
public class Order : LadderEntityBase
{
public Order() : this(Guid.NewGuid())
{
}
public Order(Guid guid) : base(guid)
{
}
public string OrderFrom { get; set; }
}
public class Parcel : LadderEntityBase
{
public Parcel() : this(Guid.NewGuid())
{
}
public Parcel(Guid guid) :base( guid)
{
}
public string SentTo { get; set; }
}