继承和共享方法 [英] Inheritance and Shared Methods

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

问题描述

1。我有一个具有一定功能的Base类。

2.然后我有一个继承自Base类的CodeSmith生成的类

并添加了功能。 />
3.由于我希望能够在第2级重新生成类,我有一个继承自它们的第3级

并实现特定的手动编码

方法。

4.我的同事让我创建了一个继承第3级
级别的第4级课程,这样他就可以编写自定义功能来覆盖简单的

以上所有级别的功能。


我将1-3作为共享方法实现。

对我来说,这样可以实现更简单的UI代码。


例如:

strSQL = Level3ClassName.Select


这样我不需要实例化类的实例我只需要使用

1-3级的共享方法。


======== ========================================== ==== =====

我的问题(除了是新的,并没有真正理解这一点

够了< g> )

我不知道如何以允许我使用的方式实现4级

级别1-3中的共享方法然后在级别4中覆盖它们。


不可能吗?

======================== ========================== ============


如果我必须改变1-3级你会推荐什么?


======================== ========================


示例代码:


级别1

公共MustInherit类别基础


公共共享功能DoSomething(ByVal Source As String)字符串


结束功能

======================================= =========

等级2

公共MustInherit类GeneratedStuff


继承基础


公共共享函数DoSomethingElse(ByVal Source As String)As String


结束函数

========= =======================================

等级3

公共MustInherit类HandCodedStuff


继承GeneratedStuff


公共共享函数IWroteThis(ByVal Source As String)As String


结束功能

============================== ==================

等级4(实现与3级相同的功能,但也允许通过覆盖自定义

更改方法或创建特定于

单个客户的新方法。 3级功能适用于所有客户。)

Public MustInherit Class FinalLevel


继承HandCodedStuff


覆盖函数DoSomethingElse (ByVal Source As String)作为字符串


结束功能


感谢您的任何建议!

- < br $>
Joe Fallon


1. I have a Base class that has a certain amount of functionality.
2. Then I have a CodeSmith generated class that inherits from the Base class
and adds functionality.
3. Since I want to be able to re-generate the classes on level 2 I have a
3rd level that inherits from them and implements specific hand coded
methods.
4. My colleague asked me to create a 4th level class that inherits the 3rd
level class so he can write custom functionality that overrides the "plain"
functionality at all levels above it.

I have 1-3 implemented as Shared methods.
To me, this enables simpler UI code.

For example:
strSQL = Level3ClassName.Select

In this way I do not need to instantiate an instance of the class I just use
the shared methods in levels 1-3.

================================================== =========
My problem (other than being new and not really understanding this well
enough yet <g> )
is I don''t know how to implement Level 4 in a way that allows me to use
Shared methods in levels1-3 and yet Override them in Level 4.

Is it not possible?
================================================== ============

If I have to change Levels 1-3 what would you recommend?

================================================

Sample code:

Level 1
Public MustInherit Class Base

Public Shared Function DoSomething(ByVal Source As String) As String

End Function
================================================
Level 2
Public MustInherit Class GeneratedStuff

Inherits Base

Public Shared Function DoSomethingElse(ByVal Source As String) As String

End Function
================================================
Level 3
Public MustInherit Class HandCodedStuff

Inherits GeneratedStuff

Public Shared Function IWroteThis(ByVal Source As String) As String

End Function
================================================
Level 4 (implements same functionality as Level 3 but also allows customized
changes by overriding methods or creating new ones that are specific to a
single client. Level 3 functionality is for all clients.)
Public MustInherit Class FinalLevel

Inherits HandCodedStuff

Overrides Function DoSomethingElse(ByVal Source As String) As String

End Function

Thanks for any advice!
--
Joe Fallon


推荐答案

我认为如果你想要在第4课使用1-3类的共享方法

,你会说myBase.method。在第4课你可以定义一个Overrides方法来覆盖

基类中的共享方法。

" Joe Fallon" < JF ****** @ nospamtwcny.rr.com>在消息中写道

news:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
I would think that in the 4th class if you wanted to use the shared methods
from classes 1-3, you would say "myBase.method" and in the 4th class you
could define an Overrides method that overrides the shared method in the
base class.
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
1。我有一个具有一定功能的Base类。
2.然后我有一个CodeSmith生成的类继承自Base
类并添加功能。
3.因为我想要能够在第2级重新生成类我有一个继承自它的第3级并实现特定的手工编码方法。
4.我的同事让我创建了第4级继承第3级级别的类,这样他就可以编写覆盖
" plain"的自定义功能。它上面的所有级别的功能。

我有1-3作为共享方法实现。
对我来说,这可以实现更简单的UI代码。

例如:
strSQL = Level3ClassName.Select

这样我就不需要实例化类的实例我只需要
使用级别1-3中的共享方法。

============================================== ==== =========
我的问题(除了是新的,并没有真正理解这一点
足够的< g>)
我不是知道如何以允许我在级别1-3中使用
共享方法的方式实现级别4,然后在级别4中覆盖它们。

是不可能的?
================================================== ============

如果我必须改变1-3级你会推荐什么?

======== ========================================

示例代码:

1级
公共MustInherit班级基地

Public Shared Fu作为字符串的DoSomething(ByVal Source As String)

结束函数

====================== ==========================
2级
公共MustInherit类GeneratedStuff

继承基数

公共共享功能DoSomethingElse(ByVal Source As String)As String

结束功能

============ ====================================
3级
公共MustInherit类HandCodedStuff

继承GeneratedStuff

公共共享函数IWroteThis(ByVal Source As String)As String

结束函数

== ==============================================
级别4(实现与级别3相同的功能,但也允许通过覆盖方法或创建特定于单个客户端的新方法来进行
自定义更改。 3级功能适用于所有客户端。)
公共MustInherit类FinalLevel

继承HandCodedStuff

覆盖函数DoSomethingElse(ByVal Source As String)As String

结束功能

感谢您的任何建议!
-
Joe Fallon
1. I have a Base class that has a certain amount of functionality.
2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality.
3. Since I want to be able to re-generate the classes on level 2 I have a
3rd level that inherits from them and implements specific hand coded
methods.
4. My colleague asked me to create a 4th level class that inherits the 3rd
level class so he can write custom functionality that overrides the "plain" functionality at all levels above it.

I have 1-3 implemented as Shared methods.
To me, this enables simpler UI code.

For example:
strSQL = Level3ClassName.Select

In this way I do not need to instantiate an instance of the class I just use the shared methods in levels 1-3.

================================================== =========
My problem (other than being new and not really understanding this well
enough yet <g> )
is I don''t know how to implement Level 4 in a way that allows me to use
Shared methods in levels1-3 and yet Override them in Level 4.

Is it not possible?
================================================== ============

If I have to change Levels 1-3 what would you recommend?

================================================

Sample code:

Level 1
Public MustInherit Class Base

Public Shared Function DoSomething(ByVal Source As String) As String

End Function
================================================
Level 2
Public MustInherit Class GeneratedStuff

Inherits Base

Public Shared Function DoSomethingElse(ByVal Source As String) As String

End Function
================================================
Level 3
Public MustInherit Class HandCodedStuff

Inherits GeneratedStuff

Public Shared Function IWroteThis(ByVal Source As String) As String

End Function
================================================
Level 4 (implements same functionality as Level 3 but also allows customized changes by overriding methods or creating new ones that are specific to a
single client. Level 3 functionality is for all clients.)
Public MustInherit Class FinalLevel

Inherits HandCodedStuff

Overrides Function DoSomethingElse(ByVal Source As String) As String

End Function

Thanks for any advice!
--
Joe Fallon


你好乔,


如果你想把你的方法保持为共享,那么你就不能在

Level 4中使用Overrides。覆盖仅适用于对象方法,因此

将告别您的共享..


但是,可以选择使用Shadows。在FinalLevel中,

阴影函数DoSomethingElse(...)As String

将为该类的用户提供名称DoSomethingElse。


如果您使用对象方法和覆盖进行此操作,您可以使用MyBase.SameMethod调用

基类方法,如Scott指出的那样。


使用共享方法,相当于使用基础名称

类。


所以你可以:

' '2级

公共MustInherit类GeneratedStuff

继承基础

公共共享函数DoSomethingElse(...)As String

结束功能

结束班级


''等级4

公共MustInherit等级FinalLevel_Client1

继承HandCodedStuff

阴影公共共享函数DoSomethingElse(...)As String

GeneratedStuff.DoSomethingElse(...)''如有必要:

''Client1'是另一回事。

结束功能

结束课程


''同样适用于Client2等。


问候,

Fergus
Hi Joe,

If you want to keep your methods as Shared then you can''t use Overrides in
Level 4. Overrideable and Overrides are only available with object methods, so
that would be goodbye to your Shared..

There is, however, the option of using Shadows. In FinalLevel,
Shadows Function DoSomethingElse(...) As String
will make the name DoSomethingElse available to users of that class.

If you were doing this using object methods and Overrides, you would call
the base class method using MyBase.SameMethod as Scott pointed out.

With Shared methods the equivalent is simply to use the name of the base
class.

So you could have:
''Level 2
Public MustInherit Class GeneratedStuff
Inherits Base
Public Shared Function DoSomethingElse(...) As String
End Function
End Class

''Level 4
Public MustInherit Class FinalLevel_Client1
Inherits HandCodedStuff
Shadows Public Shared Function DoSomethingElse(...) As String
GeneratedStuff.DoSomethingElse(...) ''If necessary:
''Do Client1''s something else.
End Function
End Class

''Ditto for Client2, etc.

Regards,
Fergus


*" Joe Fallon" < JF ****** @ nospamtwcny.rr.com> scripsit:
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
1.我有一个具有一定功能的Base类。
2.然后我有一个继承自Base类的CodeSmith生成的类
并添加功能。
3.由于我希望能够在第2级重新生成类,因此我有一个继承自它们的第3级并实现特定的手工编码方法。
4.我的同事让我创建了一个继承第3级课程的第4级课程,这样他就可以编写自定义功能,覆盖其上面所有级别的简单功能。

我有1-3作为共享方法实现。
对我来说,这可以实现更简单的UI代码。
1. I have a Base class that has a certain amount of functionality.
2. Then I have a CodeSmith generated class that inherits from the Base class
and adds functionality.
3. Since I want to be able to re-generate the classes on level 2 I have a
3rd level that inherits from them and implements specific hand coded
methods.
4. My colleague asked me to create a 4th level class that inherits the 3rd
level class so he can write custom functionality that overrides the "plain"
functionality at all levels above it.

I have 1-3 implemented as Shared methods.
To me, this enables simpler UI code.




你不能覆盖共享方法。我建议从声明中删除

''Shared'',并实现类似Singleton

设计模式。如果您想获得有关Singleton模式的更多

信息,您可以进行Google搜索。


-

Herfried K Wagner

MVP·VB Classic,VB.NET

< http://www.mvps.org/dotnet>



You cannot override a shared method. I would suggest to remove the
''Shared'' from the declaration and implement something like the Singleton
design pattern. You can make a Google search if you want to get more
information on the Singleton pattern.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>


这篇关于继承和共享方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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