本文介绍了通过继承和阴影的多态性(冲突?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 看一下这段代码: Class clsAnagrafica Public Overridable ReadOnly Property Codice()As Integer Get 返回1 结束获取 结束物业 结束班级 Class clsCliente Inherits clsAnagrafica Public Overrides ReadOnly Property Codice()As Integer 获取 返回2 结束获取 结束物业 结束班级 班级clsDocumento Public Anagrafica As new clsAnagrafica End Class Class clsDocumentoCliente Inherits clsDocumento Public Shadows Anagrafica As new clsCliente End Class 模块模块1 Sub Main() Dim A As clsAnagrafica A =新clsCliente Debug.WriteLine(A.Codice) Dim D as clsDocumento D =新clsDocumentoCliente Debug.Write Line(D.GetType) Debug.WriteLine(D.Anagrafica.Codice) End Sub 结束模块 A.Codice返回2(因为被覆盖的成员)。 D.GetType显然返回clsDocumentoCliente。 但奇怪的是D.Anagrafica.Codice返回1. 我不明白为什么! clsDocumentoCliente.Anagrafica shadows clsDocumento.Anagrafica 所以编译器应该调用clsDocumentoCliente.Anagrafica.Codice。 (作为MSDN 声明相同的基本方法可以执行不同的操作,具体取决于调用方法的实例的运行时类型)和运行 - 时间 类型是clsDocumentoCliente。 但是我发现这种行为与D的初始 声明有些相关。 事实如果我将D声明为对象D.Anagrafica.Codice返回2.(因为它应该是 ) 任何人都可以解释我为什么会这样? 提前谢谢,Lino。Take a look at this code:Class clsAnagraficaPublic Overridable ReadOnly Property Codice() As IntegerGetReturn 1End GetEnd PropertyEnd ClassClass clsClienteInherits clsAnagraficaPublic Overrides ReadOnly Property Codice() As IntegerGetReturn 2End GetEnd PropertyEnd ClassClass clsDocumentoPublic Anagrafica As New clsAnagraficaEnd ClassClass clsDocumentoClienteInherits clsDocumentoPublic Shadows Anagrafica As New clsClienteEnd ClassModule Module1Sub Main()Dim A As clsAnagraficaA = New clsClienteDebug.WriteLine(A.Codice)Dim D As clsDocumentoD = New clsDocumentoClienteDebug.WriteLine(D.GetType)Debug.WriteLine(D.Anagrafica.Codice)End SubEnd ModuleA.Codice returns 2 (because of Overridden member).D.GetType obviously returns clsDocumentoCliente.But the strange thing is that D.Anagrafica.Codice returns 1.I can''t understand why!clsDocumentoCliente.Anagrafica shadows clsDocumento.Anagraficaso the compiler should call clsDocumentoCliente.Anagrafica.Codice. (as MSDNstates "the same base method can perform different actions depending on therun-time type of the instance that invokes the method") and the run-timetype is clsDocumentoCliente.However I find that this behaviour is somewhat related to the initialdeclaration of D.Infact if I declare D as object D.Anagrafica.Codice returns 2. (as it shouldbe)Can anyone explain me why this happens?Thanks in advance, Lino.推荐答案 两件事: - 字段访问从不是多态的,方法调用可以。 Anagrafica 是一个字段。 - 使用Shadows * break * polymorphism - 这就是它的全部目的。如果你想要多态,你应该使用Overridable / Overrides。 Mattias - Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ 请只回复新闻组。Two things:- Field access is never polymorphic, methods calls can be. Anagraficais a field.- Using Shadows *breaks* polymorphism - that''s its whole purpose. Ifyou want polymorphism, you should use Overridable/Overrides instead.Mattias--Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/Please reply only to the newsgroup. 这篇关于通过继承和阴影的多态性(冲突?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-11 05:37