我对这个程序有一个小问题。该程序旨在使用多态性。我写了一个基类和两个派生类。
我们应该创建一个基类(银行账户)数组并用三个银行账户对象填充它。然后我们使用它的重载构造函数为每个银行账户对象分配一个新对象。
public partial class Form1 : Form
{
//Base class array
BankAcct[] b = new BankAcct[3];
public Form1()
{
InitializeComponent();
//This is not getting current values from form!
int accountNum;
int atmNum;
int pinNum;
an = Convert.ToInt32(accountNumber.Text);
p = Convert.ToInt32(pin.Text);
atm = an - p;
//base class
b[0] = new BankAcct(name.Text, 500.00M, accountNum);
//this derived class inherits from bankAcct name, account number, and
//the decimal which is the balance assigned to the Account
//private variables are atm and pin in this class
b[1]= new SilverBankAcct(name.Text, an, 1500.00M, atmNumber, pinNum);
//this derived class inherits from SilverBankAcct atm, pin,
//has one private variable the decimal at the end which is the interest
b[2] = new GoldBankAcct(name.Text, accountNum, 25000.00M, atm, pinNum, 0.05M);
}
我的问题是,当我在 Form1 构造函数中实例化我的对象时,字段不会从表单更新,并且这些字段中的当前值被忽略。我尝试通过访问我的基类中的属性并从那里的表单中分配值来分配信息,但是当我尝试更新我的 atm 号码和 pin 号码时,问题就出现了,它们是进入我的 SilverBankAcct 类和 GoldBankAcct 类的私有变量.
private void button1_Click(object sender, EventArgs e)
{
b[0].fName = name.Text;
b[1].fName = name.Text;
b[2].fName = name.Text;
//do the same for account number which works, but how am I supposed to update atm and pin from the form when I have no access to these variables I only have access to the base class?
}
什么是更好的方法来确保在单击按钮时将传递的值更新为表单中的当前值?