我是C#的初学者,我有一个基于使用类的继承和多态相关的特定问题。我工作的任务是一个银行账户,我必须使用三个类 - 一个是基类,另外两个是派生类。
基类被称为" BankAccount"并且两个派生类是" CheckingAccount"和" SavingsAccount"。此外,我已经为" CheckingAccount"和" SavingAccount"并且它们存储在List类中。之后,我尝试使用组合框" SelectedIndexChange"来根据派生类的属性相应地填充我的作业GUI标签。事件。
问题在于,程序不知道是否检查列表类的存储元素是否被识别为特定类型。下面是我的程序代码和类。
private void comboBoxAccountNumber_SelectedIndexChanged(object sender, EventArgs e)
{
if (selectedBankAccount[0] is CheckingAccount)
{
labelOwnerID.Text = selectedBankAccount[0].AccountNumber;
labelBalance.Text = selectedBankAccount[0].Balance.ToString("c");
}
else if (selectedBankAccount[1] is CheckingAccount)
{
labelOwnerID.Text = selectedBankAccount[1].AccountNumber;
labelBalance.Text = selectedBankAccount[1].Balance.ToString("c");
}
}
List<BankAccount> selectedBankAccount = new List<BankAccount> ();
SavingsAccount savs1_Account;
CheckingAccount chek1_Account;
SavingsAccount savs2_Account;
CheckingAccount chek2_Account;
private void FormBankOfYourSelf_Load(object sender, EventArgs e)
{
savs1_Account = new SavingsAccount("0001", "31-1000", 100m, 0.01);
chek1_Account = new CheckingAccount("0001", "44-1000", 250m, true);
savs2_Account = new SavingsAccount("0002", "31-1001", 1000m, 0.0125);
chek2_Account = new CheckingAccount("0002", "44-1001", 500m, false);
selectedBankAccount.Add(chek1_Account);
selectedBankAccount.Add(chek2_Account);
comboBoxAccountNumber.Items.Add(selectedBankAccount[0].AccountNumber);
comboBoxAccountNumber.Items.Add(selectedBankAccount[1].AccountNumber);
}
public abstract class BankAccount
{
// Fields - The data we want to store
// Naming convention for fields is to use underscore before the name
protected string _customerId;
protected string _accountNumber;
protected decimal _balance;
//Properties - Allow access to fields (Get/Set)
// Get = read access
// Set = write (modify) access
public string CustomerId
{
get { return _customerId; }
set { _customerId = value; }
}
public string AccountNumber
{
get { return _accountNumber; }
set { _accountNumber = value; }
}
public decimal Balance
{
get { return _balance; }
}
// Methods - The action or behaviors the class can do
// almost always, define the constructor and ToString methods
// Constructor creates (instantiates a new object)
public BankAccount(string customerId, string accountNumber, decimal initialBalance)
{
//fields are set = to the parameters(inputs from the form)
_customerId = customerId;
_accountNumber = accountNumber;
_balance = initialBalance;
}
public abstract bool Deposit(decimal depositAmount);
//{
// // If the depositAmount is less then 0 then RETURN false
// if (depositAmount <= 0)
// {
// return false;
// }
// // Otherwise, complete the deposit and RETURN true
// _balance += depositAmount;
// return true;
//}
public abstract bool Withdraw(decimal withdrawAmount);
//{
// // If the withdrawAmount is greater than the balance or less then or equal to 0
// // then RETURN false (don't allow withdrawal)
// if (withdrawAmount > _balance || withdrawAmount <= 0)
// {
// return false;
// }
// // Otherwise, complete the withdrawal and return true
// _balance -= withdrawAmount;
// return true;
//}
public class CheckingAccount : BankAccount
{
//private string _customerID;
//private string _accountNum;
//private decimal _initBalance;
private bool _overdraftProtection;
public CheckingAccount(string customerId, string accountNum, decimal initialBalance, bool overDraft)
:base(customerId, accountNum, initialBalance)
{
//_customerID = customerId;
//_accountNum = accountNum;
//_initBalance = initialBalance;
_overdraftProtection = overDraft;
}
public bool OverDraftProtection
{
get { return _overdraftProtection; }
set { _overdraftProtection = value; }
}
public override bool Deposit(decimal depositAmount)
{
if(depositAmount > 0)
{
_balance += depositAmount;
return true;
}
else
{
return false;
}
}
public override bool Withdraw(decimal withdrawAmount)
{
if(withdrawAmount <= _balance || withdrawAmount > 0 )
{
_balance -= withdrawAmount;
return true;
}
else
{
return false;
}
}
}
**更新:我注意到当我运行程序时,即使我在组合框中选择了不同的项目,它也不会相应地将值更改为所选项目。下面是我的程序运行时选择了不同的项目。
First selected item in combobox
Second selected item in combobox
答案 0 :(得分:0)
您的列表属于BankAccount
类型,因此您需要在属性可用之前将对象强制转换为您要使用的特定继承类
使用is
关键字然后使用as
关键字是明智的,因为您基本上是两次投放。最好使用as
关键字并测试null
CheckingAccount account = selectedBankAccount[0] as CheckingAccount
if(account != null)
{
//account is of type CheckingAccount so the additional
//CheckingAccount properties should be available via the account variable
}
答案 1 :(得分:0)
从发布的评论中可以看出问题是代码没有选择ComboBox中选择的BankAccount
。不知道如何填充ComboBox。最简单的方法是使用所选索引,假设使用selectedBankAccount集合填充comboBoxAccountNumber。
private void comboBoxAccountNumber_SelectedIndexChanged(object sender, EventArgs e)
{
BackAccount account = selectedBankAccount[comboBoxAccountNumber.SelectedIndex]
labelOwnerID.Text = account.AccountNumber;
labelBalance.Text = account.Balance.ToString("c");
}