未调用派生类中的代码 [英] Code in derived class not called

查看:57
本文介绍了未调用派生类中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请有人解释一下这种行为:


我有一个MustInherit Base类和一个继承类的继承类和

在基类中对方法进行阴影处理。


如果我对Derived类型的变量进行Dim并将其作为Derived中的代码导入

派生派生类。但是,如果我将变量调整为类型Base但新增

中的代码派生类是在所有情况下调用。


Public MustInherit Class Base

Public Sub DoSomething()

MsgBox(" Base")< br $>
结束子

结束班


公共类Class1

继承基地


Public Shadows Sub DoSomething()

MsgBox(" Class1")

End Sub

End Class
Dim x As Base = New Class1

x.DoSomething()''" Base"显示


Dim y As Class1 = New Class1

y.DoSomething()''" Class1"显示


非常感谢您的评论。


Julia。

Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() '' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() '' "Class1" displayed

Many thanks for your comments.

Julia.

推荐答案

这是正确的行为。想一想 - 如果你的基类没有使用DoSomething方法,你甚至可以通过基类变量调用DoSomething

吗?绝对不是 - 只有当你把它投射到

派生类时,你才能做到这一点。基类变量可以和

只能访问基类中的成员,即使该对象是派​​生类的

实例。


希望有帮助..

Imran。


" Julia" <菊*** @ discussions.microsoft.com>在消息中写道

新闻:A4 ********************************** @ microsof t.com ...
That is correct behaviour. Think about it - if your base class didn''t have
the DoSomething method, would you even have been able to call DoSomething
via the base class variable? Definitely not - only if you cast it to the
derived class would you be able to do that. The base class variable can and
will only access members in the base class even though the object is an
instance of the derived class.

hope that helps..
Imran.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...


请有人解释一下这个行为:

我有一个MustInherit Base类和一个继承的Derived类Base和
在基类中对方法进行阴影处理。

如果我对Derived类型的变量进行Dim,并将其作为Derived,则调用
Derived类中的代码。但是,如果我将变量调整为类型Base但是
新建它作为Derived类型调用Base类代码 - 我希望在所有实例中调用派生类中的代码


Public MustInherit Class Base
Public Sub DoSomething()
MsgBox(" Base")
End Sub
End Class

公开Class Class1
继承基地

公共阴影Sub DoSomething()
MsgBox(" Class1")
End Sub
End Class

Dim x As Base = New Class1
x.DoSomething()''" Base"显示

Dim y As Class1 = New Class1
y.DoSomething()''" Class1"显示

非常感谢您的评论。

Julia。
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() '' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() '' "Class1" displayed

Many thanks for your comments.

Julia.



这是正确的行为。想一想 - 如果你的基类没有使用DoSomething方法,你甚至可以通过基类变量调用DoSomething

吗?绝对不是 - 只有当你把它投射到

派生类时,你才能做到这一点。基类变量可以和

只能访问基类中的成员,即使该对象是派​​生类的

实例。


希望有帮助..

Imran。


" Julia" <菊*** @ discussions.microsoft.com>在消息中写道

新闻:A4 ********************************** @ microsof t.com ...
That is correct behaviour. Think about it - if your base class didn''t have
the DoSomething method, would you even have been able to call DoSomething
via the base class variable? Definitely not - only if you cast it to the
derived class would you be able to do that. The base class variable can and
will only access members in the base class even though the object is an
instance of the derived class.

hope that helps..
Imran.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...


请有人解释一下这个行为:

我有一个MustInherit Base类和一个继承的Derived类Base和
在基类中对方法进行阴影处理。

如果我对Derived类型的变量进行Dim,并将其作为Derived,则调用
Derived类中的代码。但是,如果我将变量调整为类型Base但是
新建它作为Derived类型调用Base类代码 - 我希望在所有实例中调用派生类中的代码


Public MustInherit Class Base
Public Sub DoSomething()
MsgBox(" Base")
End Sub
End Class

公开Class Class1
继承基地

公共阴影Sub DoSomething()
MsgBox(" Class1")
End Sub
End Class

Dim x As Base = New Class1
x.DoSomething()''" Base"显示

Dim y As Class1 = New Class1
y.DoSomething()''" Class1"显示

非常感谢您的评论。

Julia。
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() '' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() '' "Class1" displayed

Many thanks for your comments.

Julia.



查看Overridable和Overrides,这将给你你想要的行为
寻找。

" Julia" <菊*** @ discussions.microsoft.com>在消息中写道

新闻:A4 ********************************** @ microsof t.com ...
Look into Overridable and Overrides, this will give you the behavior you are
looking for.
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...


请有人解释一下这个行为:

我有一个MustInherit Base类和一个继承的Derived类Base和
在基类中对方法进行阴影处理。

如果我对Derived类型的变量进行Dim,并将其作为Derived,则调用
Derived类中的代码。但是,如果我将变量调整为类型Base但是
新建它作为Derived类型调用Base类代码 - 我希望在所有实例中调用派生类中的代码


Public MustInherit Class Base
Public Sub DoSomething()
MsgBox(" Base")
End Sub
End Class

公开Class Class1
继承基地

公共阴影Sub DoSomething()
MsgBox(" Class1")
End Sub
End Class

Dim x As Base = New Class1
x.DoSomething()''" Base"显示

Dim y As Class1 = New Class1
y.DoSomething()''" Class1"显示

非常感谢您的评论。

朱莉娅。
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() '' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() '' "Class1" displayed

Many thanks for your comments.

Julia.



这篇关于未调用派生类中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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