选项严格和后期绑定问题 [英] Option Strict and late binding problem

查看:59
本文介绍了选项严格和后期绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我已经完成了我的作业并在Google网上论坛上查了一下但是这个

似乎还没有涵盖的情况。


这里是场景...


有两个类,Foo和Bar(实际上有两个以上的
类涉及但是两个就足以解释问题),每个

类都有一个Copy()方法如下:

公共函数Copy()As Object实现ICloneable.Clone

''返回Foo对象的副本

结束函数


公共函数Copy()As Object实现ICloneable.Clone

''返回一个Bar对象的副本

结束函数


我有另一个继承ArrayList的类。这个类允许列表

包含Foo和Bar对象。


到目前为止和我在一起?这里是踢球者...


然后我按照以下方式迭代ArrayList,制作数组的副本

list:


Dim objekt,objcopy As Object

For each objekt in myArrayListInstance

objcopy = objekt.Copy()''每个objekt都是Foo对象或者酒吧

对象

''构建另一个ArrayList,其中包含原始所有对象的副本

ArrayList

Next <使用''Option Strict On''会产生编译错误。


如何使用''Option Strict实现这种多态性在''?


感谢您的考虑,


Daniel Klein

Cuyahoga Falls,OH

I think I''ve done my homework and checked around on Google Groups but this
seems to be a situation not yet covered.

Here is the scenario...

There are two classes, Foo and Bar (actually there are more than two
classes involved but two will suffice to explain the problem), and each
class has a Copy() method as follows:

Public Function Copy() As Object Implements ICloneable.Clone
'' returns a copy of a Foo object
End Function

Public Function Copy() As Object Implements ICloneable.Clone
'' returns a copy of a Bar object
End Function

I have another class that subclasses ArrayList. This class allows the list
to contain Foo and Bar objects.

With me so far? Here comes the kicker...

I then iterate over the ArrayList as follows making a copy of the array
list:

Dim objekt, objcopy As Object
For Each objekt In myArrayListInstance
objcopy = objekt.Copy() '' Each objekt is either a Foo object or a Bar
object
'' build another ArrayList with copies of all objects from the original
ArrayList
Next

With ''Option Strict On'' this produces a compiler error.

How can I achieve this kind of polymorphism with ''Option Strict On'' ?

Thanks for your consideration,

Daniel Klein
Cuyahoga Falls, OH

推荐答案

>如何通过''Option Strict On'实现这种多态性?


For每个objekt As ICloneable in myArrayListInstance

objcopy = objekt.Clone()


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
< a rel =nofollowhref =http://www.msjogren.net/dotnet/target =_ blank> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。
>How can I achieve this kind of polymorphism with ''Option Strict On'' ?

For Each objekt As ICloneable In myArrayListInstance
objcopy = objekt.Clone()

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


" Daniel Klein" <哒*********** @ hotmail.com>在留言中写道

新闻:k5 ******************************** @ 4ax.com ...
"Daniel Klein" <da***********@hotmail.com> wrote in message
news:k5********************************@4ax.com...
使用''Option Strict On''会产生编译错误。
我怎样才能实现这种多态...?
With ''Option Strict On'' this produces a compiler error.
How can I achieve this kind of polymorphism ... ?




" Object"并不像你想象的那么有用 - 在操作对象时总是试着获得

a正确的类型,并且因为两个(所有?)的

你的类实现了IClonable,就像你需要使用它们时一样好的类型,如:


For each objekt as IClonable in myArrayListInstance

objcopy = objekt.Clone()''返回Foo或Bar

''用副本构建另一个ArrayList

NewArrayList.Add(objekt)

下一页


HTH,

Phill W.



"Object" isn''t half as useful as you might think - always try to get at
a proper type when manipulating objects and, since both (all?) of
your classes implement IClonable, that''s as good a type as any when
you need to use them, as in :

For Each objekt As IClonable In myArrayListInstance
objcopy = objekt.Clone() '' returns either Foo or Bar
'' build another ArrayList with copies
NewArrayList.Add( objekt )
Next

HTH,
Phill W.


Mttias ,


感谢您的回复。


不幸的是,这不会奏效。每个Copy()方法都比Object.Clone()做的要多一些,这就是创建我的

自己的实现的原因。并且每个Copy()方法都无法重构为

a常见的实现。


此外,这个问题解决了如何实现真正的更通用的方法<没有后期约束的
多态性。


欢迎任何其他想法。


Daniel Klein

Cuyahoga Falls,OH

星期二,2005年2月1日01:46:03 +0100,Mattias Sj?gren

< ma ******* *************@mvps.org>写道:
Mttias,

Thank you for the cortesy of your reply.

Unfortunately, this won''t work. Each of the Copy() methods is doing a bit
more than what Object.Clone() is doing, which is the reason for creating my
own implementation. And each of the Copy() methods cannot be refactored to
a common implementation.

Besides, this problem address a more generic one of how to achieve true
polymorphism without late binding.

Any other ideas are welcome.

Daniel Klein
Cuyahoga Falls, OH
On Tue, 01 Feb 2005 01:46:03 +0100, Mattias Sj?gren
<ma********************@mvps.org> wrote:
如何通过''Option Strict On'实现这种多态性?
How can I achieve this kind of polymorphism with ''Option Strict On'' ?


对于每个objekt作为ICloneable in myArrayListInstance
objcopy = objekt.Clone()

Mattias



For Each objekt As ICloneable In myArrayListInstance
objcopy = objekt.Clone()

Mattias






这篇关于选项严格和后期绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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