多态性 [英] Polymorphism

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

问题描述

嗨!


我在vb.net中与多态性有点挣扎。有一些我知道的事情

我可以用C ++做,但我似乎无法在vb.net中做到这一点(或c#为那个

问题)。


基本上,我有一个抽象基类,用于处理添加,删除,

编辑另一个类。


我写的该类的一个实现,以及''托管'类的衍生版本

- 所以我的衍生版本实际上管理了一个

专用版本的基础版本确实。但是,编译器

抱怨我没有实现MustInherit方法...


要更好地解释一下......

公共类CMessage

''这是管理的class


Public MustInherit Class CMessageManager

''这是抽象基类

Public MustOverride函数MessageReceived(ByRef objMessage As

CMessage)As Boolean


公共类CEmail继承CMessage

''派生版


公共类CEmailMessageManager继承CMessageManager

''已实现的版本 - 希望它处理电子邮件


公共MustOverride函数MessageReceived(ByRef objMessage as CEmail)

作为布尔值

''传入电子邮件并处理它

C ++支持这个概念 - 必须可以这样做。 NET,但是

我真的不知道怎么做。


任何帮助都将不胜感激!


解决方案

文章< O


************** @ TK2MSFTNGP09.phx。 gbl>,Richard Tappenden写道:

嗨!

我在vb.net中与多态性有点挣扎。有一些我知道的东西
我可以用C ++做,但我似乎无法在vb.net中做到这一点(或者对于那个问题而言是c#)。

基本上,我有一个抽象基类,用于处理添加,删除,编辑另一个类。

我已经编写了该类的实现,以及''托管的派生版本''类 - 所以我的派生版本有效地管理了一个基本版本的专用版本。但是,编译器抱怨我没有实现MustInherit方法...

要更好地解释一下......

Public Class CMessage
''这是管理的class

公共MustInherit类CMessageManager
''这是抽象基类
公共MustOverride函数MessageReceived(ByRef objMessage As
CMessage)As Boolean
<公共类CEmail继承CMessage
''派生版
公共类CEmailMessageManager继承CMessageManager
''已实现的版本 - 希望它处理电子邮件
As Boolean
''传入电子邮件并处理它

C ++支持这个概念 - 它必须是可以在.NET中实现,但是
我真的不知道如何。

任何帮助都将不胜感激!




我不确定是什么问题 - 编译器正在告诉你

问题是什么...如果你想能够使用

CEmailMessageManager那么你必须覆盖MessageReceived

fu ..
公共类CEmailMessageManager继承CMessageManager


公共覆盖函数MessageReceived(ByRef objMessage As

CEmail)作为布尔值

''做东西

结束功能

结束班级


即使在C ++中是规则。你不能使用没有

实现的函数。在VB.NET中使用抽象基类的规则非常相似,而且在C ++中也是如此。

-

Tom Shelton

MVP [Visual Basic]


嗨Tom,


感谢您的回复 - 但我我想你已经误解了这个问题 - 我是

我知道我可以创建一个抽象类的实例等...


问题是我似乎无法覆盖基类方法,除非

他们都使用基本消息类来执行操作 - 即:


基类method =

公共MustOverride函数MessageReceived(ByRef objMessage作为CMessage)

作为布尔值


我的派生版本

公共覆盖函数MessageReceived(ByRef objMessage As CEmailMessage)

As Boolean

''做点什么

结束函数。


被覆盖的版本抱怨 - 说明我仍然需要覆盖

MessageReceived(ByRef objMessage CMessage)作为布尔值。

在C ++中,你可以这样做 - 即它正确解析虚拟表。


例如 - 检查这些c ++文件......(如果他们看起来不好看,我会道歉,我很快就将他们击倒以证明这一点) />

" Tom Shelton" < to*@mtogden.com>在消息中写道

news:uQ ************** @ TK2MSFTNGP09.phx.gbl ...

在文章< O中

Hi!

I''m struggling a bit with Polymorphism in vb.net. There is something I know
I can do in C++, but I cannot seem to do it in vb.net (or c# for that
matter).

Basically, I have an abstract base class that deals with adding, deleting,
editing another class.

I have written an implementation of that class, and also a derived version
of the ''managed'' class - so effectively my derived version manages a
specialised version of whatever the base one does. However, the compiler
complains that I havent implemented the MustInherit methods...

To explain a bit better...
Public Class CMessage
'' This is the "managed" class

Public MustInherit Class CMessageManager
'' This is the abstract base class
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

Public Class CEmail Inherits CMessage
'' Derived version

Public Class CEmailMessageManager Inherits CMessageManager
'' Implemented version - want it to deal with emails

Public MustOverride Function MessageReceived (ByRef objMessage As CEmail)
As Boolean
'' Pass in an email and deal with it
This concept is supported by C++ - it must be possible to do it in .NET, but
I really dont know how.

Any help would be greatly appreciated!


解决方案

In article <O


**************@TK2MSFTNGP09.phx.gbl>, Richard Tappenden wrote:

Hi!

I''m struggling a bit with Polymorphism in vb.net. There is something I know
I can do in C++, but I cannot seem to do it in vb.net (or c# for that
matter).

Basically, I have an abstract base class that deals with adding, deleting,
editing another class.

I have written an implementation of that class, and also a derived version
of the ''managed'' class - so effectively my derived version manages a
specialised version of whatever the base one does. However, the compiler
complains that I havent implemented the MustInherit methods...

To explain a bit better...
Public Class CMessage
'' This is the "managed" class

Public MustInherit Class CMessageManager
'' This is the abstract base class
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

Public Class CEmail Inherits CMessage
'' Derived version

Public Class CEmailMessageManager Inherits CMessageManager
'' Implemented version - want it to deal with emails

Public MustOverride Function MessageReceived (ByRef objMessage As CEmail)
As Boolean
'' Pass in an email and deal with it
This concept is supported by C++ - it must be possible to do it in .NET, but
I really dont know how.

Any help would be greatly appreciated!



I''m not sure what the problem is - the compiler is telling you exactly
what the problem is... If you want to be able to use
CEmailMessageManager then you must override the MessageReceived
function..

Public Class CEmailMessageManager Inherits CMessageManager

Public Overrides Function MessageReceived (ByRef objMessage As
CEmail) As Boolean
'' Do stuff
End Function
End Class

Even in C++ this is the rule. You can''t use a function that has no
implementation. The rules for using Abstract base classes are pretty
much the same in VB.NET as the are in C++.
--
Tom Shelton
MVP [Visual Basic]


Hi Tom,

Thanks for the reply - but I think you''ve misunderstood the question - I''m
well aware that I can create an instance of an abstract class etc...

The problem is that I cant seem to override the base class methods unless
they all use the base message class to do the operations - i.e:

The base class method =
Public MustOverride Function MessageReceived (ByRef objMessage As CMessage)
As Boolean

My derived version
Public Overrides Function MessageReceived(ByRef objMessage As CEmailMessage)
As Boolean
'' Do something
End Function.

The overridden version complains - stating that I still need to override
MessageReceived(ByRef objMessage CMessage) As Boolean.

In C++, you can do this - i.e. it resolves the virtual table correctly.

For example - check these c++ files...(apologies if they don''t look nice, I
knocked them up quickly to prove the point)

"Tom Shelton" <to*@mtogden.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...

In article <O


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

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