gpt4 book ai didi

c# - WPF 和 C# : Trouble with Classes and Interfaces

转载 作者:行者123 更新时间:2023-11-30 13:57:07 24 4
gpt4 key购买 nike

我正在开发一个银行账户程序。除了主窗口类之外,我还使用了一个基类、一个接口(interface)和两个派生类。我正在制作一个 WPF 应用程序,到目前为止,我能够在该应用程序中使用类对象的 ArrayList 填充一个 ListBox 就好了。但是,当我去修改 ArrayList 中的任何对象时,我在正确地重新填充 ListBox 时遇到了困难。任何帮助将不胜感激!

这是我的主窗口代码:

 public partial class MainWindow : Window
{
ArrayList bankAccountList = new ArrayList();

BankAccount savingsAccount = new SavingsAccount("New", "Account", "newaccount");
BankAccount checkingAccount = new CheckingAccount("New", "Account", "newaccount");
IAccount iAccount;
string typeOfAccount = "";

public MainWindow()
{
InitializeComponent();
}

//When the user pushes the "Add A Saving Account" button, a new savings account is added to the ArrayList and displayed in the app.
private void btnAddAccount_Click(object sender, RoutedEventArgs e)
{
iAccount = (IAccount)savingsAccount;

savingsAccount.Deposit(0.00m);

bankAccountList.Add(savingsAccount);
lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
typeOfAccount = "savings";
}

//When the user pushes the "Add A Checking Account" button, a new checking account is added to the ArrayList and displayed in the app.
private void btnAddCheckingAccount_Click(object sender, RoutedEventArgs e)
{
iAccount = (IAccount)checkingAccount;

checkingAccount.Deposit(0.00m);

bankAccountList.Add(checkingAccount);
lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
typeOfAccount = "checking";
}

//When the user pushes the "Delete Account" button, the account is removed, and this change is shown in the app.
private void btnDeleteAccount_Click(object sender, RoutedEventArgs e)
{
lbxExistingAccounts.Items.RemoveAt(lbxExistingAccounts.Items.IndexOf(lbxExistingAccounts.SelectedItem));
}

//The user can push the "Submit Changes" button to submit his or her changes to the number and name of the account.
private void btnSubmitChanges_Click(object sender, RoutedEventArgs e)
{
try
{

for (int index = 0; index < bankAccountList.Count; index++)
{
if (index == lbxExistingAccounts.SelectedIndex)
{
if (typeOfAccount == "savings")
{
savingsAccount.AccountNumber = tbxAccountNumber.Text;
savingsAccount.AccountOwnerFirstName = tbxFirstName.Text;
savingsAccount.AccountOwnerLastName = tbxLastName.Text;
}

else if (typeOfAccount == "checking")
{
checkingAccount.AccountNumber = tbxAccountNumber.Text;
checkingAccount.AccountOwnerFirstName = tbxFirstName.Text;
checkingAccount.AccountOwnerLastName = tbxLastName.Text;
}
}
}
lbxExistingAccounts.Items.Clear();
foreach (object accountObject in bankAccountList)
{
lbxExistingAccounts.Items.Add(accountObject);
}
}
catch (FormatException)
{
MessageBox.Show("You may enter changes as letters, numbers, or both.");
}

}

这是我的界面代码:

interface IAccount
{
void SetAccountBalance(decimal accountBalance);
string AccountInformation();
}

这是我的基类代码:

    abstract class BankAccount
{
public string AccountNumber { get; set; }
public string AccountOwnerFirstName { get; set; }
public string AccountOwnerLastName { get; set; }
public decimal AccountBalance { get; set; }
public decimal AnnualInteresetRate { get; set; }
public string TypeOfAccount { get; set; }

public BankAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
{
AccountOwnerFirstName = accountOwnerFirstName;
AccountOwnerLastName = accountOwnerLastName;
AccountNumber = accountNumber;
}

public abstract void Deposit(decimal amount);

public abstract void Withdraw(decimal amount);

public decimal CalculateInterest()
{
return (this.AccountBalance * this.AnnualInteresetRate) / 100;
}
}

这是我的派生类之一。我做的都差不多。

    class SavingsAccount : BankAccount, IAccount
{
public SavingsAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
: base(accountOwnerFirstName, accountOwnerLastName, accountNumber)
{
AnnualInteresetRate = 0.95m;
}

public override void Deposit(decimal amount)
{
AccountBalance = AccountBalance + amount;
}

public override void Withdraw(decimal amount)
{
AccountBalance = AccountBalance - amount;
}

public void SetAccountBalance(decimal accountBalance)
{
AccountBalance = accountBalance;
}

public string AccountInformation()
{
return "Savings Account \n " + AccountOwnerFirstName + " " + AccountOwnerLastName + ", Account#: " + AccountNumber + ", Balance: $" + AccountBalance;
}
}

最佳答案

看起来您才刚刚起步,这很好,因为您将要重新做一些事情。

  1. 您不需要将派生对象转换为它们的基类型。这种类型的转换称为“向上转换”,无需任何转换即可自动工作。

  2. 您发布的代码高度“类似于 WinForms”,这在 WPF 中不是一个好的方法。首先让您的帐户列表成为一个 ObservableCollection 并将您的 ListBox 的 ItemsSource 绑定(bind)到它。

属性看起来像:

public ObservableCollection<BankAccount> Accounts {get; set;}

为简洁起见,实际上应该使用 INotifyPropertyChanged 和绑定(bind):

<ListBox "ItemsSource={Binding Accounts}"/>

当然,这应该放在 View 模型类中,但您可以通过设置代码隐藏开始:

DataContext = this;

一旦一切正常,您的所有文本框都应该绑定(bind)到数据对象或 View 本身的属性。

这里有一篇关于将 MVVM 与 WPF 结合使用的精彩教程 (MSDN)。相信我,使用这种设计模式将使您使用 WPF 的工作变得非常容易、更具可扩展性,并且您会发现 WPF 基本上就是为使用它而设计的。使用 WinForms 方法只会让您感到痛苦。

如果我能提供进一步帮助或澄清任何问题,请告诉我!

关于c# - WPF 和 C# : Trouble with Classes and Interfaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com