共享方法之前未调用的共享构造函数 [英] Shared constructor not called before Shared method

查看:53
本文介绍了共享方法之前未调用的共享构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个派生类的基类(我正在写作
VB.NET)。我希望每个派生类都有一个唯一的类ID(一个

String),我希望派生类继承自基础

类*共享*方法使用类ID。


所以我给了基类一个classID字段,然后我给了

派生类共享构造函数,我以前用过将classID

字段设置为每个派生类的适当值。但是这个
不起作用!在下面的示例中,方法


DerivedClass.PrintClassID()


打印基类的类ID,而不是派生的

类。因此,派生类的共享构造函数(正确初始化那个

类的继承共享方法所需的是
)不会被调用。


但是如果我在派生的

类中创建一个实例(在示例中是xyz),那么共享构造函数*会被调用,即使我什么都不做

与类实例。这似乎是错的。人们会期望

一个类的共享字段应该在

之前初始化任何类共享方法被调用,并且这个初始化

应该不依赖于创建一个实际的类实例。


那么是怎么回事?这是一个突出的错误,还是一个错误的人们已经让自己与之生活在一起,或者我只是误解了一个功能?我该怎么办才能得到我想要的

结果?我可以使用Shadow的PrintClassID方法,然后它就可以按预期工作了
。但是我不想要暗示使用classID字段的每个方法

- 整点就是所有那些

功能完全由基类,没有

派生类需要知道任何东西。


有什么想法吗?


''=====开始示例=====

导入系统


类BaseClass

受保护的共享classID As String =" BaseClass!"


公共共享子PrintClassID()

Console.WriteLine(" classID ="& classID)

结束次级

结束班级


类DerivedClass

继承BaseClass


共享子新()

classID =" DerivedClass!"

End Sub

End Class

模块TestModule

Public Sub Main()

''Dim xyz As New DerivedClass

DerivedClass.PrintClassID()


结束子

结束模块

''=====结束示例=====

-

John Brock
jb****@panix.com

I have a base class with several derived classes (I''m writing in
VB.NET). I want each derived class to have a unique class ID (a
String), and I want the derived classes to inherit from the base
class *Shared* methods which make use of the class ID.

So I gave the base class a classID field, and then I gave the
derived classes Shared constructors, which I used to set the classID
field to the appropriate values for each derived class. But this
doesn''t work! In the example below the method

DerivedClass.PrintClassID()

prints the class ID for the base class, rather than the derived
class. So the Shared constructor for the derived class, which is
needed to properly initialize the inherited Shared methods of that
class, is not getting called.

However if I create an instance (xyz in the example) of the derived
class the Shared constructor *does* get called, even if I do nothing
with the class instance. This just seems wrong. One would expect
that the Shared fields of a class should get initialized before
any class Shared methods get called, and that this initialization
should not depend on the creation of an actual instance of the class.

So what is going on? Is this an outstanding bug, or a misfeature
that people have resigned themselves to living with, or have I
simply misunderstood a feature? And what should I do to get the
result I want? I can Shadow the PrintClassID method, and then it
works as expected. But I don''t want to have to Shadow every method
which uses the classID field -- the whole point was that all that
functionality was to be handled entirely by the base class, without
the derived classes needing to know anything abou it.

Any thoughts?

''===== Begin Example =====
Imports System

Class BaseClass
Protected Shared classID As String = "BaseClass!"

Public Shared Sub PrintClassID()
Console.WriteLine("classID = " & classID)
End Sub
End Class

Class DerivedClass
Inherits BaseClass

Shared Sub New()
classID = "DerivedClass!"
End Sub
End Class
Module TestModule
Public Sub Main()

''Dim xyz As New DerivedClass
DerivedClass.PrintClassID()

End Sub
End Module
''===== End Example =====
--
John Brock
jb****@panix.com

推荐答案

John,
John,
DerivedClass.PrintClassID()


这只是编译成一个调用BaseClass.PrintClassID,并且

永远不必加载DerivedClass。


我该怎么做才能得到我想要的结果?
DerivedClass.PrintClassID()
This simply compiles to a call to BaseClass.PrintClassID, and
DerivedClass never has to be loaded.

And what should I do to get the result I want?




如果你想要多态,你应该使用实例成员而不是

的共享。

Mattias

-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



If you want polymorphism you should be using instance members instead
of Shared.
Mattias

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


在文章< Ox ************** @ TK2MSFTNGP09.phx.gbl>,

Mattias Sj?gren< ma ******************** @ mvps.org>写道:
In article <Ox**************@TK2MSFTNGP09.phx.gbl>,
Mattias Sj?gren <ma********************@mvps.org> wrote:
John,
John,
DerivedClass.PrintClassID()
这只是编译为对BaseClass的调用。 PrintClassID和
从不必加载DerivedClass。


但我明确要求加载它!由于这些是共享的

方法,因此无需担心打字,并且没有任何内容

阻止我随时调用BaseClass.PrintClassID,如果

这就是我想要的。


在Paul Vick的优秀VB.NET书的第181页,他注意到

一般规则拇指是一个共享的构造函数将运行

之前可以访问任何可以依赖它的东西。这个

让我相信.NET会在我看来是自然的b / b
的东西,即如果一个类有一个共享的构造函数,它会

运行以在运行任何类''共享

方法之前初始化类。

我该怎么做才能得到我想要的结果?
DerivedClass.PrintClassID() This simply compiles to a call to BaseClass.PrintClassID, and
DerivedClass never has to be loaded.
But I explicitly asked for it to be loaded! Since these are shared
methods there is no concern about typing, and there is nothing
stopping me from calling BaseClass.PrintClassID at any time, if
that were what I wanted.

On page 181 of Paul Vick''s excellent VB.NET book he notes that "the
general rule of thumb is that a the shared constructor will be run
before anything that could depend on it can be accessed". This
led me to believe that .NET would do what seems to me the natural
thing, which is that if a class had a shared constructor, it would
be run to initialize the class before any of the classes'' shared
methods were run.
And what should I do to get the result I want?



如果你想要多态,你应该使用实例成员而不是共享。


If you want polymorphism you should be using instance members instead
of Shared.




当然,你可以*总是*使用实例成员而不是共享。

那么.NET框架的共享

成员究竟是什么意思呢?这是因为某些信息真的属于

类而不是实例,并且很方便

能够访问该信息而无需前往麻烦

创建一个实例。在这种情况下,我将不得不创建

我的派生类的实际实例,我只会使用

来获取有关类本身的元信息。这不是很难,但看起来有点傻。


我想如果你不能这样做那么你就不能这样做。但是我仍然很好奇这是否是一种疏忽 - 语言中的缺陷

- 或者是否有一些很好的理由为什么我想要

会导致问题,*不应该被允许。你有什么理由是什么,或者你知道任何网页

讨论这个问题吗?

-

John Brock
jb****@panix.com




" John Brock" <>在消息新闻中写道:dg ********** @ reader1.panix.com ...



:我有几个基类派生类(我写的是

:VB.NET)。我希望每个派生类都有一个唯一的类ID(

:String),我希望派生类继承自基础

:class * Shared * methods它使用了类ID。



:所以我给了基类一个classID字段,然后我给了

:派生类共享构造函数,我用它将classID

:field设置为每个派生类的适当值。但这个

:不起作用!在下面的示例中方法



:DerivedClass.PrintClassID()



:打印类基类的ID,而不是派生的

:类。所以派生类的共享构造函数,即

:正确初始化那个

:class的继承共享方法所需要的,不会被调用。



:但是如果我在派生的

:类中创建一个实例(在示例中为xyz),则共享构造函数*会被调用,即使我什么都不做

:用类实例。这似乎是错的。人们会期望

:类的共享字段应该在

之前初始化:任何类共享方法都被调用,并且这个初始化

:不应该依赖于创建一个实际的类实例。



:那是怎么回事?这是一个突出的错误,还是一个错误的错误

:人们已经辞职了,或者我有点b $ b:只是误解了一个功能?我应该怎么做才能得到

:我想要的结果?我可以使用Shadow的PrintClassID方法,然后它就可以按预期运行
:但是我不想要暗示每一种方法

:它使用classID字段 - 整点是所有那些

:功能是要处理的完全由基类,没有

:派生类需要知道任何事情。



:有什么想法?



:''=====开始示例=====

:进口系统

:< br $>
:Class BaseClass

:受保护的共享classID As String =" BaseClass!"



:Public Shared Sub PrintClassID()

:Console.WriteLine(" classID ="&classID)

:End Sub

:End Class



:类DerivedClass

:继承BaseClass



:共享Sub New()

:classID =" DerivedClass!"

:End Sub

:End Class





:模块TestModule

:Public Sub Main()



:''Dim xyz As New DerivedClass

:DerivedClass.PrintClassID()



:End Sub

:结束模块

:''=====结束示例=====

: -

:John Brock

注意,我相信您应该避免在这些帖子中使用您的实际电子邮件地址

,因为它们可以被机器人扫描在

中寻找它们以包含在垃圾邮件列表中。我可能弄错了,但我不认为

如此。掩盖您的电子邮件地址(例如: jB****@REMOVEpanix.com

那些线)。这应该是您的sig

块以及您在新阅读器中使用的电子邮件地址的情况。

无论如何,以下情况如何?

---------------------------------------

选项严格


进口Microsoft.VisualBasic

进口系统


公共舱基地

Public Overridable ReadOnly属性ClassID As String

获取

返回Base

结束获取

结束财产


公共可覆盖子PrintClassID()

SharedFunction(ClassID)

结束子


受保护的共享子SharedFunction(ClassID为字符串)

Console.WriteLIne(ClassID)

结束子

结束类

Public Class Derived1:Inherits Base

Public Overrides ReadOnly Property ClassID As String

Get

返回" Derived1"

结束获取

结束财产

Public Overrides Sub PrintClassID()

SharedFunction(ClassID)

End Sub

End Class

Public Class Derived2:Inherits Base

Public Overrides ReadOnly Property ClassID As String

Get

Return" Derived2"

结束获取

结束财产


公开覆盖Sub PrintClassID()

SharedFunction(ClassID)

结束次级

结束班级


公共模块[模块]

Public Sub Main

Dim b作为新基地

Dim d1 As New Derived1

Dim d2 As New Derived2


b.PrintClassID

d1.PrintClassID

d2.PrintClassID

End Sub

结束模块

---------------------------------------

这将产生以下输出:

基本

派生1

派生2

HTH

Ralf < br $> b $ b -

---------------------------- ------------------------------

* ^〜^ ^〜^ *

* _ {~~} {~~} _ *

* / _``> *< > *<'''_ _ \\ * *

*(\ --_)++)(++(_ - /)*

-------------------------------------------------- --------

合气道没有高级学生 - 只有

有能力的初学者。没有先进的技术 -

只有正确应用基本原则。

"John Brock" <> wrote in message news:dg**********@reader1.panix.com...
:
: I have a base class with several derived classes (I''m writing in
: VB.NET). I want each derived class to have a unique class ID (a
: String), and I want the derived classes to inherit from the base
: class *Shared* methods which make use of the class ID.
:
: So I gave the base class a classID field, and then I gave the
: derived classes Shared constructors, which I used to set the classID
: field to the appropriate values for each derived class. But this
: doesn''t work! In the example below the method
:
: DerivedClass.PrintClassID()
:
: prints the class ID for the base class, rather than the derived
: class. So the Shared constructor for the derived class, which is
: needed to properly initialize the inherited Shared methods of that
: class, is not getting called.
:
: However if I create an instance (xyz in the example) of the derived
: class the Shared constructor *does* get called, even if I do nothing
: with the class instance. This just seems wrong. One would expect
: that the Shared fields of a class should get initialized before
: any class Shared methods get called, and that this initialization
: should not depend on the creation of an actual instance of the class.
:
: So what is going on? Is this an outstanding bug, or a misfeature
: that people have resigned themselves to living with, or have I
: simply misunderstood a feature? And what should I do to get the
: result I want? I can Shadow the PrintClassID method, and then it
: works as expected. But I don''t want to have to Shadow every method
: which uses the classID field -- the whole point was that all that
: functionality was to be handled entirely by the base class, without
: the derived classes needing to know anything abou it.
:
: Any thoughts?
:
: ''===== Begin Example =====
: Imports System
:
: Class BaseClass
: Protected Shared classID As String = "BaseClass!"
:
: Public Shared Sub PrintClassID()
: Console.WriteLine("classID = " & classID)
: End Sub
: End Class
:
: Class DerivedClass
: Inherits BaseClass
:
: Shared Sub New()
: classID = "DerivedClass!"
: End Sub
: End Class
:
:
: Module TestModule
: Public Sub Main()
:
: ''Dim xyz As New DerivedClass
: DerivedClass.PrintClassID()
:
: End Sub
: End Module
: ''===== End Example =====
: --
: John Brock
Note, I believe you should refrain from using your actual email address
in these posts as they can be scanned by bots that seek them out in
order to include in spam lists. I could be mistaken, but I don''t think
so. Mask your email address instead (e.g.: jB****@REMOVEpanix.com or
something along those lines). This should be the case both for your sig
blocks as well as the email address you use in your newreader.
In any event, how about the following?
---------------------------------------
Option Strict

Imports Microsoft.VisualBasic
Imports System

Public Class Base
Public Overridable ReadOnly Property ClassID As String
Get
Return "Base"
End Get
End Property

Public Overridable Sub PrintClassID()
SharedFunction(ClassID)
End Sub

Protected Shared Sub SharedFunction(ClassID As String)
Console.WriteLIne(ClassID)
End Sub
End Class

Public Class Derived1 : Inherits Base
Public Overrides ReadOnly Property ClassID As String
Get
Return "Derived1"
End Get
End Property

Public Overrides Sub PrintClassID()
SharedFunction(ClassID)
End Sub
End Class

Public Class Derived2 : Inherits Base
Public Overrides ReadOnly Property ClassID As String
Get
Return "Derived2"
End Get
End Property

Public Overrides Sub PrintClassID()
SharedFunction(ClassID)
End Sub
End Class

Public Module [module]
Public Sub Main
Dim b As New Base
Dim d1 As New Derived1
Dim d2 As New Derived2

b.PrintClassID
d1.PrintClassID
d2.PrintClassID
End Sub
End Module
---------------------------------------
This will generate the following output:
Base
Derived1
Derived2
HTH
Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.


这篇关于共享方法之前未调用的共享构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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