inheritance of static data
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; }
}
精彩评论