C# 重写c中的基本保护属性#
轻微的新手问题 我有一个基本的付款类。除了额外的属性外,所有共享相同的属性。其中一个属性是C# 重写c中的基本保护属性#,c#,oop,inheritance,C#,Oop,Inheritance,轻微的新手问题 我有一个基本的付款类。除了额外的属性外,所有共享相同的属性。其中一个属性是postrl。在基类中,这是空的,但在子类中,每个类都有自己的url。不允许从类外部访问它,它是固定的,不应更改。如何覆盖子类中的属性 e、 g 我该怎么做呢 提前感谢如果您将您的姿势变成实际的虚拟财产,您应该能够完成它,如下所示: class paymentBase { public int transactionId {get;set;} public string item {get;
postrl
。在基类中,这是空的,但在子类中,每个类都有自己的url。不允许从类外部访问它,它是固定的,不应更改。如何覆盖子类中的属性
e、 g
我该怎么做呢
提前感谢如果您将您的
姿势
变成实际的虚拟财产,您应该能够完成它,如下所示:
class paymentBase
{
public int transactionId {get;set;}
public string item {get;set;}
protected virtual postUrl { get { return String.Empty; }}
public void payme();
}
class paymentGateWayNamePayment : paymentBase
{
protected override postUrl {get { return "http://myurl.com/payme"; } }
}
根据您的需求,我建议使用接口,因为postrl是一个通用属性,可以用于任何东西,例如页面回发、控制回发、您的类可能会使用它等等。 任何类都可以根据需要使用此接口
interface IPostUrl
{
string postUrl { get; }
}
class paymentBase
{
public int transactionId {get;set;}
public string item {get;set;}
public void payme(){}
}
class paymentGateWayNamePayment : paymentBase, IPostUrl
{
public string postUrl
{
get { return "http://myurl.com/payme"; }
}
}
我知道这是一个延迟输入,但是如果您希望子类设置一次postrl值,然后再也不设置它,那么您需要将其作为基类的私有值
abstract class paymentBase
{
public paymentBase(string postUrl) { this.postUrl = postUrl; }
public int transactionId { get; set; }
public string item { get; set; }
protected string postUrl { get; private set; }
public void payme();
}
class paymentGateWayNamePayment : paymentBase
{
public paymentGateWayNamePayment() : base("http://myurl.com/payme") { }
}
这不是一个属性,而是一个字段。请发布可以原封不动地复制和编译的代码。您可能还应该将基类抽象化。
abstract class paymentBase
{
public paymentBase(string postUrl) { this.postUrl = postUrl; }
public int transactionId { get; set; }
public string item { get; set; }
protected string postUrl { get; private set; }
public void payme();
}
class paymentGateWayNamePayment : paymentBase
{
public paymentGateWayNamePayment() : base("http://myurl.com/payme") { }
}