多态性&集合 [英] Polymorphism & Collections

查看:92
本文介绍了多态性&集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#和多态性相对较新,所以我想要实现的目标可能是不可能的,或者我可能有一种完全不同的方法

应该花费。


我有一个基类(MyBase)和一组MyBase对象

(MyBaseCollection)。现在,我还扩展了MyBase(MyExtendedBase

:MyBase),它具有其他属性。最后,我有一个收集

的MyExtendedBase对象(MyExtendedBaseCollection:MyBaseCollection)。


我有一个数据层,有一个方法(GetMyBaseCollection)检索
来自数据库的
记录,并使用这些

记录填充MyBaseCollection。但是,在某些情况下我需要使用此方法来填充MyExtendedBaseCollection
。但是这里抓住了 - 我希望我的数据

层不知道(和独立)的MyExtendedBase和

MyExtendedBaseCollection。


我已经尝试了几种不同的方法来实现这一点,但无论我做什么,我最终都需要将MyBase上传到MyExtendedBase,这是非法的
$ b C#中的$ b。我需要我的数据层能够接受并填充一个带有MyBase对象的

MyBaseCollection(它可能是一个

MyExtendedBaseCollection的子类)(可能是它们) sub

类MyExtendedBase),然后当收集集合时,集合中的

项目属于合适的类型(取决于类型

of collection in)。


我读过不同的设计模式(即Decorator和

Composite),但我不确定它们是否适用根据我的情况,如果是这样的话,如何实现它们。


再一次,我可能会做一些完全不合逻辑的事情,所以我很开放你可能有任何建议。此外,如果我没有提供足够的信息,请告诉我

我会跟进。


提前致谢。

Jerad

I''m relatively new to C# and polymorphism, so what I''m trying to accomplish
may not be possible, or there may be a totally different approach that I
should be taking.

I have a base class (MyBase) and a collection of MyBase objects
(MyBaseCollection). Now, I also have an extension to MyBase (MyExtendedBase
: MyBase) that has additional properties. And lastly, I have a collection
of MyExtendedBase objects(MyExtendedBaseCollection : MyBaseCollection).

I have a data layer that has a method (GetMyBaseCollection) that retrieves
records from the database, and populates a MyBaseCollection with these
records. However, there are cases where I need to use this method to
populate a MyExtendedBaseCollection. But here''s the catch -- I want my data
layer to be unaware (and independent) of MyExtendedBase and
MyExtendedBaseCollection.

I have tried several different ways to accomplish this, but no matter what I
do, I end up needing to up-cast MyBase to MyExtendedBase, which is illegal
in C#. I need my data layer to be able to accept and populate a
MyBaseCollection (with it possibly being a sub class of a
MyExtendedBaseCollection) with MyBase objects (with them possibly being sub
classes of MyExtendedBase), and then when the collection is returned, the
items in the collection are of the appropriate type (depending on the type
of collection passed in).

I have read about different design patterns (namely Decorator and
Composite), but am not sure if they apply to my situation, and if so, how to
implement them accordingly.

Again, I may be doing something totally illogical, so I''m open to any
suggestions you may have. Also, if I have not provided enough info, let me
know and I will follow up.

Thanks in advance.

Jerad

推荐答案

我认为你们非常接近。如果你有一个基类,那么你应该

将MyExtendedBaseCollection作为MyBaseCollection

对象传递没有问题。这是多态的力量。


你的几句陈述:
I think your''re pretty close. If you have a base class, then you should
have no problem passing MyExtendedBaseCollection as a MyBaseCollection
object. This is the power of polymorphism.

A couple of your statements:
需要我的数据层能够接受并填充一个
MyBaseCollection (它可能是一个MyExtendedBaseCollection的子类)和MyBase对象(它们可能是MyExtendedBase的子类),
need my data layer to be able to accept and populate a
MyBaseCollection (with it possibly being a sub class of a
MyExtendedBaseCollection) with MyBase objects (with them possibly being sub
classes of MyExtendedBase),




MyExtendedBase和MyExtendedBaseCollection是MyBase的子类

和MyBaseCollection。


当你说子类时它意味着它继承自基类

或抽象类。我认为你的术语是向后的。


如果你想把MyBase强制转换为MyExtendedBase,那就不会工作了。当你需要传递一个特殊类型的类时,但是

不想编写其他方法(除了特殊功能)

然后你转换你的dervied或子类(MyExtendedBase) )键入

MyBase,你可以正常传递它,你在子类中重写的基类中的任何方法都可以被称为

正常,因为基类和子类都有定义

这些方法。


实现此目的的另一种方法是使用接口。


就设计模式而言,这是我还在学习的东西。

很好,我发现如果你不懂设计模式

完全,然后根本不使用它。因为很有可能你是b / b
会模仿这种模式,然后产生更多令人头疼的问题。


希望我的评论有所帮助!


Jerad Rose写道:
Jerad Rose wrote:
do,我最终需要将MyBase上传到MyExtendedBase,这是非法的


这是一个低调。它是合法的 (静态有效),但如果实际转换的对象实例不是MyExtendedBase的实例,则转换

将失败并抛出异常。

再一次,我可能会做一些完全不合逻辑的事情,所以我对你可能提出的任何建议持开放态度。另外,如果我没有提供足够的信息,那么让我知道,我会跟进。
do, I end up needing to up-cast MyBase to MyExtendedBase, which is illegal
That''s a down-cast. It''s "legal" (staticly valid), but if the actual
object instance casted is not an instance of MyExtendedBase the cast
will fail and throw an exception.
Again, I may be doing something totally illogical, so I''m open to any
suggestions you may have. Also, if I have not provided enough info, let me
know and I will follow up.




让数据层,应该是通用的类型,接受

数据结构将项目填充为参数。如果您的数据层

需要构建未知数据从数据中键入对象你可以看看工厂模式的
。数据层可以接受作为参数的工厂给出要解析的数据并从中创建一个对象。


顺便说一句:是你在C#1还是2?


装饰师和复合材料与你的问题完全无关。


-

Helge Jensen

mailto:他********** @ slog.dk

sip:他********** @ slog.dk

- =>塞巴斯蒂安的封面音乐: http://ungdomshus.nu < = -



Let the data-layer, that should be generic in the types, accept the
data-structure to fill items into as an argument. If your data layer
needs to construct "unknown" type objects from data you can have a look
at the factory-pattern. The data-layer can accept, as a parameter, a
factory that is given the data to parse and create an object from that.

BTW: Are you in C#1 or 2?

Decorator and composite are completely unrelated to your problem.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


您好Jerad,

您只需要一个集合。基类的一个。

当你从数据库中提取数据时,在实例化之前测试

标记Mybase和MyExtendedBase之间的差异的属性

要添加到集合中的对象。

bool MyDatabaseGetRoutine(ref collection col)

{

SQlDataCommand等设置。

MyBase b;

而MyDatareader.read

{

If(MyDataReader。 GetString(say5)== dbNull)//实例化MyBase

b = new MyBase();

else //实例化MyBaseExtended

b = new MyBaseExtended();

//现在填写公共(MyBase)属性

b.Common1 = MyDataRead.GetString(0);

b.Common2 = MyDataRead.GetString(1);

再次测试类型。

if(b.GetType()。ToString()==" MyBaseExtended")


{


//填写扩展属性


}


col.ad D b);无论实例化如何,都可以添加到集合中。


当您使用该集合时,使用基本类型迭代它

变量


bool LetsUnpackIt(收藏col)


foreach(mybase b in col)


{


if(b.gettype()。ToString()==" MyBase")


//做mybase的事情


else


//做myBaseExtended的事情


HTH


鲍勃

" Jerad Rose" < no@spam.com>在消息中写道

新闻:ea ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Jerad,
You only need one collection. The one for the base class.
When you''re pulling in data from the database test the attribute(s) that
mark the difference between Mybase and MyExtendedBase before instantiating
the object that you are going to add to the collection.
bool MyDatabaseGetRoutine(ref collection col)
{
SQlDataCommand etc setup.
MyBase b;
While MyDatareader.read
{
If(MyDataReader.GetString(say5) == dbNull) //Instantiate a MyBase
b=new MyBase();
else // Instantiate a MyBaseExtended
b= new MyBaseExtended();
// Now fill the common (MyBase) properties
b.Common1 = MyDataRead.GetString(0);
b.Common2 = MyDataRead.GetString(1);
Test again for type.
if (b.GetType().ToString()=="MyBaseExtended")

{

// Fill the extended properties

}

col.add(b); Adding to collection regardless of Instantiation.

When you come to use the collection iterate across it with a base type
variable

bool LetsUnpackIt(collection col)

foreach (mybase b in col)

{

if (b.gettype().ToString()=="MyBase")

//Do the mybase things

else

// Do the myBaseExtended things

HTH

Bob

"Jerad Rose" <no@spam.com> wrote in message
news:ea**************@TK2MSFTNGP09.phx.gbl...
我是相对的C#和多态的新手,所以我想要实现
的成就可能是不可能的,或者我可能会采取完全不同的方法。

我有一个基类(MyBase)和一组MyBase对象
(MyBaseCollection)。现在,我还有MyBase
(MyExtendedBase:MyBase)的扩展,它具有其他属性。最后,我有一个MyExtendedBase对象的集合(MyExtendedBaseCollection:MyBaseCollection)。

我有一个数据层,它有一个方法(GetMyBaseCollection)从数据库中检索
记录,并使用这些
记录填充MyBaseCollection。但是,有些情况下我需要使用此方法来填充MyExtendedBaseCollection。但是这里有捕获 - 我希望我的
数据层不知道(和独立)MyExtendedBase和
MyExtendedBaseCollection。

我尝试了几种不同的方法来实现这,但无论我做什么
,我最终都需要将MyBase上传到MyExtendedBase,这在C#中是非法的。我需要我的数据层能够使用MyBase对象接受并填充一个
MyBaseCollection(它可能是一个MyExtendedBaseCollection的子类)(它们可能是
MyExtendedBase的子类) ),然后当收集集合时,集合中的
项目是合适的类型(取决于传入的集合的类型)。

我已阅读关于不同的设计模式(即Decorator和
Composite),但我不确定它们是否适用于我的情况,如果是这样,那么如何实现


再次,我可能正在做一些完全不合逻辑的事情,所以我愿意接受你可能提出的任何建议。另外,如果我没有提供足够的信息,请告诉我
我会跟进。

提前致谢。

Jerad
I''m relatively new to C# and polymorphism, so what I''m trying to accomplish may not be possible, or there may be a totally different approach that I
should be taking.

I have a base class (MyBase) and a collection of MyBase objects
(MyBaseCollection). Now, I also have an extension to MyBase (MyExtendedBase : MyBase) that has additional properties. And lastly, I have a collection
of MyExtendedBase objects(MyExtendedBaseCollection : MyBaseCollection).

I have a data layer that has a method (GetMyBaseCollection) that retrieves
records from the database, and populates a MyBaseCollection with these
records. However, there are cases where I need to use this method to
populate a MyExtendedBaseCollection. But here''s the catch -- I want my data layer to be unaware (and independent) of MyExtendedBase and
MyExtendedBaseCollection.

I have tried several different ways to accomplish this, but no matter what I do, I end up needing to up-cast MyBase to MyExtendedBase, which is illegal
in C#. I need my data layer to be able to accept and populate a
MyBaseCollection (with it possibly being a sub class of a
MyExtendedBaseCollection) with MyBase objects (with them possibly being sub classes of MyExtendedBase), and then when the collection is returned, the
items in the collection are of the appropriate type (depending on the type
of collection passed in).

I have read about different design patterns (namely Decorator and
Composite), but am not sure if they apply to my situation, and if so, how to implement them accordingly.

Again, I may be doing something totally illogical, so I''m open to any
suggestions you may have. Also, if I have not provided enough info, let me know and I will follow up.

Thanks in advance.

Jerad



这篇关于多态性&amp;集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆