我正在创建一个程序,其中包含我不太明白的这两种方法。它们是“取款”和“存款”,它们位于 CheckingAccount 类中。在这些方法中,我希望最初的值为 0,然后添加到它。然后我想取新的数字并从中减去。我想“存入”250 美元。然后我要'提取'98美元。我不确定在哪里存储这些值以及如何执行它们。当我将取款和存款方法留空时,我知道显示应该如何显示。
账户类别:
class Account
{
protected string firstName;
protected string lastName;
protected long number;
public string FirstName
{
set
{
firstName = value;
}
}
public string LastName
{
set
{
lastName = value;
}
}
public long Number
{
set
{
number = value;
}
}
public override string ToString()
{
return firstName + " " + lastName + "\nAccount #: " + number;
}
}
}
支票账户类别:
class CheckingAccount : Account
{
private decimal balance;
public CheckingAccount(string firstName, string lastName, long number, decimal initialBalance)
{
FirstName = firstName;
LastName = lastName;
Number = number;
Balance = initialBalance;
}
public decimal Balance
{
get
{
return balance;
}
set
{
balance = value;
}
}
public void deposit(decimal amount)
{
//initial value should be 0 and should be adding 250 to it.
}
public void withdraw(decimal amount)
{
//this takes the 250 amount and subtracts 98 from it
}
public void display()
{
Console.WriteLine(ToString());
Console.WriteLine("Balance: ${0}", Balance);
}
}
}
显示类:
class Display
{
static void Main(string[] args)
{
CheckingAccount check = new CheckingAccount("John", "Smith", 123456, 0M);
Console.WriteLine("After Account Creation...");
check.display();
Console.WriteLine("After Depositing $250...");
//constructor
Console.WriteLine("After Withdrawing $98...");
//constructor
}
}
}
我希望我的输出看起来像这样:
创建帐户后...
约翰·史密斯
帐号:123456
余额:0
存入 250 美元后...
约翰·史密斯
帐号:123456
余额:250
提取 98 美元后...
约翰·史密斯
帐号:123456
余额:152
最佳答案
简单的答案是
public void deposit(decimal amount)
{
balance += amount;
}
public void withdraw(decimal amount)
{
balance -= amount;
}
随意添加必要的验证( overdraw ?试图存入负数?)
关于c# - 具有 2 种方法的简单银行程序,我无法完全解析 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211777/