VB.NET重载问题/问题 [英] VB.NET Overloading problem/question

查看:45
本文介绍了VB.NET重载问题/问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了重载问题。例如:


公共舱基地

...

结束班级


公共类派生继承基础

...

结束班


公共舱客户

Public Sub MethodX()

Dim obj as Base = new Derived()

MethodY(obj)

End Sub


Public Sub MethodY(arg as Base)

...

End Sub


Public Sub MethodY (arg as Derived)

...

End Sub

结束班级


MethodX电话具有Base作为参数的MethodY,即使obj是

实际上是Derived对象。具有Derived作为参数的MethodY似乎与我更接近。
似乎更接近我。在我的实际情况中,我在其他地方得到了

派生对象,并且不知道它是什么类型,而不是它是Base的子类的

子类,所以我无法投射它进一步来说。我本质上是试图以多态方式对真实对象类型做一个选择...案例。任何

的建议?


谢谢,

Brian Haynes

I am having a problem with overloading. An example:

Public Class Base
...
End Class

Public Class Derived Inherits Base
...
End Class

Public Class Client
Public Sub MethodX()
Dim obj as Base = new Derived()
MethodY(obj)
End Sub

Public Sub MethodY(arg as Base)
...
End Sub

Public Sub MethodY(arg as Derived)
...
End Sub
End Class

MethodX calls the MethodY that has a Base as a parameter, even though obj is
really a Derived object. The MethodY that has a Derived as a parameter would
seem to be a closer match to me. In my actual situation, I am getting the
Derived object elsewhere and do not know what type it is other than it is a
subclass of Base, so I cannot cast it more specifically. I am essentially
trying to do a select...case on the real object type polymorphically. Any
suggestions?

Thanks,
Brian Haynes

推荐答案

=?Utf-8?B?QnJpYW4gSGF5bmVz?=< Br ********* @ discussion.microsoft.com>

在新闻中写道:36 * ********************************* @ microsof t.com:
=?Utf-8?B?QnJpYW4gSGF5bmVz?= <Br*********@discussions.microsoft.com>
wrote in news:36**********************************@microsof t.com:
Public Sub MethodY (arg as Base)
...
End Sub

公共子方法Y(arg as Derived)
...
End Sub 结束类

方法X调用具有Base作为参数的MethodY,即使
obj实际上是Derived对象。具有派生为
Public Sub MethodY(arg as Base)
...
End Sub

Public Sub MethodY(arg as Derived)
...
End Sub
End Class

MethodX calls the MethodY that has a Base as a parameter, even though
obj is really a Derived object. The MethodY that has a Derived as a

的方法


这是正常的。根据引用的类型,而不是实例的

类型,在构建时解决重载。在运行时解决将非常缓慢。

-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是一种反击的艺术形式 ;


让你的ASP.NET应用程序运行得更快
http://www.atozed.com/IntraWeb/



This is normal. Overloads are resolved at build time based on the type of the reference, not the
type of the instance. To resolve at run time would be very slow.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/


Brian,

除了其他评论。


考虑将MethodY直接或间接移动到Base中作为可覆盖的

(直接)。


类似于:


|公共班级基地

| ...


Public Overridable Sub MethodY()

''在这里为Base的方法Y工作。

结束子


|结束班

|

|公共类派生继承基础

| ...


Public Overrides Sub MethodY()

''在这里为Derived'的方法Y工作。

结束子


|结束班

|

|公共舱客户

| Public Sub MethodX()

| Dim obj as Base = new Derived()

obj.MethodY()

|结束子

|

|最终类


如果MethodY需要行为,您可能需要将客户端作为上述参数传递

来自客户......

如果方法Y需要在客户端,然后考虑双重发送

(间接)。


|公共班级基地

| ...


Public Overridable Sub MethodY(theClient As Client)

theClient.MethodY(Me)

End Sub


|结束班

|

|公共类派生继承基础

| ...


公共覆盖Sub MethodY(客户端客户端)

theClient.MethodY(Me)

End Sub


|结束班

|

|公共舱客户

| Public Sub MethodX()

| Dim obj as Base = new Derived()

obj.MethodY(Me)

|结束子

|

| Public Sub MethodY(arg as Base)


''在这里为Base'的方法Y工作。

|结束子

|

| Public Sub MethodY(arg as Derived)


''在这里为Derived'的方法Y工作。

|结束子

|结束类


使用任何一种方法都可能会引入不需要的类之间的耦合,在这种情况下,我会考虑引入一个基础接口

或客户实施以帮助解耦课程...


希望这有帮助

Jay


" Brian Haynes" <峰; br ********* @ discussions.microsoft.com>在消息中写道

news:36 ********************************** @ microsof t.com ...

|我遇到了重载问题。例如:

|

|公共班级基地

| ......

|结束班

|

|公共类派生继承基础

| ......

|结束班

|

|公共舱客户

| Public Sub MethodX()

| Dim obj as Base = new Derived()

| MethodY(obj)

|结束子

|

| Public Sub MethodY(arg as Base)

| ......

|结束子

|

| Public Sub MethodY(arg as Derived)

| ......

|结束子

|结束班

|

| MethodX调用具有Base作为参数的MethodY,即使obj



|真的是派生的对象。具有Derived作为参数

的MethodY将

|似乎与我更接近。在我的实际情况中,我得到了

|派生对象在别处,不知道它是什么类型除了它是

a

| Base的子类,所以我不能更具体地强制转换它。我基本上是

|试图以多态方式对真实对象类型进行选择...案例。任何

|建议?

|

|谢谢,

| Brian Haynes
Brian,
In addition to the other comments.

Consider moving MethodY directly or indirectly into Base as overridable
(directly).

Something like:

| Public Class Base
| ...

Public Overridable Sub MethodY()
'' do work for Base''s Method Y here.
End Sub

| End Class
|
| Public Class Derived Inherits Base
| ...

Public Overrides Sub MethodY()
'' do work for Derived''s Method Y here.
End Sub

| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
obj.MethodY()
| End Sub
|
| End Class

You may need to pass Client as a parameter above if MethodY needs behavior
from Client...
If MethodY "needs" to be in Client, then consider a double dispatch
(indirectly).

| Public Class Base
| ...

Public Overridable Sub MethodY(theClient As Client)
theClient.MethodY(Me)
End Sub

| End Class
|
| Public Class Derived Inherits Base
| ...

Public Overrides Sub MethodY(theClient As Client)
theClient.MethodY(Me)
End Sub

| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
obj.MethodY(Me)
| End Sub
|
| Public Sub MethodY(arg as Base)

'' do work for Base''s Method Y here.
| End Sub
|
| Public Sub MethodY(arg as Derived)

'' do work for Derived''s Method Y here.
| End Sub
| End Class

Using either method may introduce coupling between the classes that is not
desirable, in which case I would consider introducing an Interface that Base
or Client implements to help decouple the classes...

Hope this helps
Jay

"Brian Haynes" <Br*********@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
|I am having a problem with overloading. An example:
|
| Public Class Base
| ...
| End Class
|
| Public Class Derived Inherits Base
| ...
| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
| MethodY(obj)
| End Sub
|
| Public Sub MethodY(arg as Base)
| ...
| End Sub
|
| Public Sub MethodY(arg as Derived)
| ...
| End Sub
| End Class
|
| MethodX calls the MethodY that has a Base as a parameter, even though obj
is
| really a Derived object. The MethodY that has a Derived as a parameter
would
| seem to be a closer match to me. In my actual situation, I am getting the
| Derived object elsewhere and do not know what type it is other than it is
a
| subclass of Base, so I cannot cast it more specifically. I am essentially
| trying to do a select...case on the real object type polymorphically. Any
| suggestions?
|
| Thanks,
| Brian Haynes




" Chad Z. Hower aka Kudzu"写道:

"Chad Z. Hower aka Kudzu" wrote:
这是正常的。根据引用的类型而不是实例的类型,在构建时解决重载。在运行时解决将非常慢。
This is normal. Overloads are resolved at build time based on the type of the reference, not the
type of the instance. To resolve at run time would be very slow.




例如:

Dim i as Object = new Integer

Debug.WriteLine(i.ToString())

输出为0,而不是System.Object。显然,ToString调用是根据对象的类型而不是引用来解析的。是不是

在引用类型上解决的重载有点不一致,并且

覆盖要在对象类型上解决?



Example:

Dim i as Object = new Integer
Debug.WriteLine(i.ToString())

The output is 0, not "System.Object". Obviously the ToString call is being
resolved based on the type of the object, not the reference. Isn''t it a
little inconsistent for overloads to be resolved on reference type and
overrides to be resolved on object type?


这篇关于VB.NET重载问题/问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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