在Base构造函数C#中使用变量

时间:2016-06-06 14:49:39

标签: c# inheritance

我有这个类和构造函数

public class BillOutcomeClass
{
    public string LargeMessage { get; set; }
    public string MobileMessage { get; set; }
    public string Value { get; set; }

    public BillOutcomeClass(string acctNumber, HttpContext currentContext)
    {
        Customer customer = CustomerSecurity.GetCustomer(currentContext);
        Account acct = customer.GetAccountForTransaction(acctNumber);
    }
}

我需要能够在PastDue类中使用AccountCustomer实例。必须能够在继承的类中访问这些类。

这是继承的类。

public class PastDue : BillOutcomeClass
{
    public PastDue (string acctNumber, HttpContext currentContext) : base (acctNumber,  currentContext)
    {
        /// I need to be able to access customer.prop, and acct.prop from the base class
    }
}

1 个答案:

答案 0 :(得分:7)

在我看来,您只需要一些protected属性:

public class BillOutcomeClass
{
    public string LargeMessage { get; set; }
    public string MobileMessage { get; set; }
    public string Value { get; set; }

    protected Customer Cust { get; set; }
    protected Account Acct { get; set; }

    public BillOutcomeClass(string acctNumber, HttpContext currentContext)
    {
        this.Cust = CustomerSecurity.GetCustomer(currentContext);
        this.Acct = customer.GetAccountForTransaction(acctNumber);
    }
}