📄 testsavingsaccounts.cs
字号:
using System;
namespace Example_1
{
class SavingsAccount
{
//用于存储帐户号码、余额和已获利息的类字段。
private int _accountNumber;
private double _balance;
private double _interestEarned;
// 利率是静态的,因为所有的帐户都使用相同的利率
private static double _interestRate;
// 构造函数初始化类成员
public SavingsAccount(int accountNumber, double balance)
{
this._accountNumber = accountNumber;
this._balance = balance;
}
// AccountNumber只读属性
public int AccountNumber
{
get
{
return _accountNumber;
}
}
// Balance 只读属性
public double Balance
{
get
{
if (_balance < 0)
Console.WriteLine("无余额");
return _balance;
}
}
// InterestEarned 读/写属性
public double InterestEarned
{
get
{
return _interestEarned;
}
set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestEarned 不能为负数");
return;
}
_interestEarned = value;
}
}
// InterestRate 读/写属性为静态,
// 因为所有特定类型的帐户都具有相同的利率
public static double InterestRate
{
get
{
return _interestRate;
}
set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestRate 不能为负数");
return;
}
else
{
_interestRate = value / 100;
}
}
}
}
class TestSavingsAccount
{
/// <摘要>
/// 应用程序的主入口点。
/// </摘要>
[STAThread]
static void Main(string[] args)
{
// 创建 SavingsAccount 的对象
SavingsAccount objSavingsAccount = new SavingsAccount(12345, 5000);;
// 用户交互
Console.WriteLine("输入到现在为止已获得的利息和利率");
objSavingsAccount.InterestEarned = Int64.Parse(Console.ReadLine());
SavingsAccount.InterestRate = Int64.Parse(Console.ReadLine());
// 使用类名访问静态属性
objSavingsAccount.InterestEarned += objSavingsAccount.Balance * SavingsAccount.InterestRate;
Console.WriteLine("获得的总利息为: {0}", objSavingsAccount.InterestEarned);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -