を使用した後にクラスのプロパティにアクセスする私はC#の初心者です。クラスの使用に基づいて継承と多相に関連する質問があります。私が働いている課題は銀行口座で、私は3つのクラスを使用する必要があります.1つは基本クラスであり、他の2つはクラスを派生したクラスです。
基本クラスは "BankAccount"と呼ばれ、2つの派生クラスは "CheckingAccount"と "SavingsAccount"です。さらに、私は "CheckingAccount"と "SavingAccount"のためのオブジェクトを宣言し、それらはListクラスに格納されています。その後、コンボボックスの "SelectedIndexChange"イベントを使用して、派生クラスのプロパティに基づいて割り当てのGUIのラベルをそれに応じて設定しようとしています。
問題は、何らかの形で、リストクラスの格納された要素が特定の型として認識されるかどうかをプログラムがチェックしないことです。以下に、プログラムとクラスのための私のコードが含まれています。
List <>
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
基本クラスのプロパティ( 'AccountNumber'と' Balance')のみを使用している場合、なぜ派生クラスが何であるか気にしますか? – Comintern