开发者

inheritance of static data

开发者 https://www.devze.com 2023-03-15 03:46 出处:网络
If have 3 classes: public abstract class BankAccount { public static decimal IntrestRate { get; set; } }

If have 3 classes:

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

}

public class SavingsAccount  : BankAccount      
{
}

public class SightDeposit  : BankAccount      
{
}

Cl开发者_StackOverflow社区ient code:

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

So in need to implement it as follows

public abstract class BankAccount
    {
}

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

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

thereby repeating IntrestRate in all derived classes :-(

Is there a way to define it once in the base-class but still make the program behave as it should ?

thank you

Chris


Remove the static modifier.

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

精彩评论

暂无评论...
验证码 换一张
取 消