问题描述
我的工作是应该模拟银行账户的程序。我有我的基类被称为投资,我的两个子类被称为股票和共同基金,我也有人称是应该要与基类投资相关的CustomerAccount一类。
I am working on a program that is supposed to simulate bank accounts. I have my base class called "Investment", my two subclasses called "Stock" and "MutualFund", and I also have a class called "CustomerAccount" that is supposed to be associated with the base class "Investment".
我不知道如何将CustomerAccount类投资类关联。
I'm not sure how to associate the CustomerAccount class with the Investment class.
推荐答案
该CustomerAccount类与投资CanHave的关系。
The CustomerAccount class has a "CanHave" relationship with Investment.
在您CustomerAccount类中,创建投资的集合。你可以使用一个列表来实现这一点:
In your CustomerAccount class, create a collection of Investments. You could use a list to achieve this:
List<Investment> _investments;
不要忘了初始化在构造函数列表中。
Don't forget to initialize the list in your constructor.
_investments = new List<Investment>();
编辑:
在列表中循环:
foreach Investment invst in _investments
{
if (invst.GetType() == typeof(Stock)) { }
else if (invst.GetType() == typeof(MutualFund)) { }
}
这篇关于类关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!