继承静态数据

时间:2011-06-22 15:25:07

标签: c# inheritance static

如果有3个班级:

public abstract class BankAccount
{
 public static decimal IntrestRate { get; set; }

}

public class SavingsAccount  : BankAccount      
{
}

public class SightDeposit  : BankAccount      
{
}

客户代码:

SavingsAccount.IntrestRate = 3.0M;
SightDeposit.IntrestRate = 1.0M;
--> will override the value of SavingsAccount.IntrestRate

因此需要按如下方式实施

public abstract class BankAccount
    {
}

public class SavingsAccount  : BankAccount      
{
    public static decimal IntrestRate { get; set; }
}

public class SightDeposit  : BankAccount      
{
    public static decimal IntrestRate { get; set; }
}

从而在所有派生类中重复IntrestRate: - (

有没有办法在基类中定义一次,但仍能使程序按原样运行?

谢谢

克里斯

1 个答案:

答案 0 :(得分:3)

删除静态修改器。

public abstract class BankAccount
{
    public decimal IntrestRate { get; set; }
}