【发布时间】:2022-01-17 17:22:11
【问题描述】:
作为 uni 作业的一部分,我需要在 C# 中创建一个自助结账模拟并添加一个附加功能 - 在本例中是一个会员卡积分系统。
我创建了一个将持有积分的成员类,但我试图让我的 winforms UI 在单击扫描会员卡按钮时显示成员名称。
GetName() 函数如下 -
class Members
{
// Attributes
protected int membershipID;
protected string firstName;
protected string lastName;
protected int clubcardPoints;
// Constructor
public Members(int membershipID, string firstName, string lastName, int clubcardPoints)
{
this.membershipID = membershipID;
this.firstName = firstName;
this.lastName = lastName;
this.clubcardPoints = clubcardPoints;
}
// Operations
public string GetName()
{
string name = firstName + " " + lastName;
return name;
}
...
}
这是来自 UserInterface.cs 中 UpdateDisplay 函数的 sn-p。
void UpdateDisplay()
{
// DONE: use all the information we have to update the UI:
// - set whether buttons are enabled
// - set label texts
// - refresh the scanned products list box
if (selfCheckout.GetCurrentMember() != null)
{
lblMember.Text = Members.GetName();
}
...
}
Members.GetName() 不能用,因为不是静态函数,但是不知道能不能转成静态函数。让这个工作的最佳方法是什么?
感谢任何帮助,我希望它有意义!对此很新,所以我不确定我的措辞是否正确。
【问题讨论】:
-
所以你想要
lblMember.Text = selfCheckout.GetCurrentMember().GetName()?