大家帮分析下
using System;
namespace SavingsAccount
{
/// <summary>
/// 创建SavingsAccount类,用Static变量annualInsterestRate来存储利率。对所有的帐户持有者,该类的每个对象都有一个Private实例变量SavingsBalance,指明储户当前存款的数量,CalculateMondifyInteres
/// t方法通过SavingsBalance乘以annualInterestRate除以12来按月计算利息。
/// </summary>
class SavingsAccount
{
private string name;//姓名
public double annualInsterestRate;//存储利率
private double SavingsBalance;//储户当前存款的数量
private double Interest;//月利息
private double Increase;//余额
public SavingsAccount(string name, double SavingsBalance)//构造函数
{
ModifyInterestRate(name,SavingsBalance);
}
public void Print()
{
Console.WriteLine("姓名:{0}",name);
Console.WriteLine("基本工资:{0}",SavingsBalance);
Console.WriteLine("用户月利息:{0:0%}",Interest);
Console.WriteLine("余额:{0,5:c}",Increase);
}
public void CalculateMondifyInterest(double annualInsterestRate)//计算利息的方法
{
Interest=annualInsterestRate*SavingsBalance/12.0;
Increase=Interest*SavingsBalance;
}
public void ModifyInterestRate(string n,double a)
{
name=n;
SavingsBalance=a;
}
}
class Program
{
public static void Main(string[] args)
{
SavingsAccount [] e=new SavingsAccount[2];//定义对象数组
e[0]=new SavingsAccount("saver1",2000.00);//使用构造方法实例化对象
e[1]=new SavingsAccount("saver2",3000.00);
e[0].CalculateMondifyInterest(0.04);
e[1].CalculateMondifyInterest(0.04);
e[0].CalculateMondifyInterest(0.05);
e[1].CalculateMondifyInterest(0.05);
foreach(SavingsAccount E in e)
{
E.Print();
Console.WriteLine();
}
Console.Read();
}
}
}
那么写下来就发现SavingsAccount类的数据成员annualInsterestRate一直没调用,就是当我分别给它赋值0.04和0.05时(e[0].CalculateMondifyInterest(0.04);e[1].CalculateMondifyInterest(0.04);e[0].CalculateMondifyInterest(0.05);[1].CalculateMondifyInterest(0.05);),但没效果啊怎么回事,不好意思,我刚学C#,太菜了