obj2 =新CCircle obj2.DifferentDraw() 结束子 结束课 谢谢 MasoudI''ve come across something in Interface implementation that I am not sure iscorrect behavior in VB.NET (and maybe C#) or not?Consider following example:Public Interface IShapeSub Draw()End InterfacePublic Class CCircleImplements IShapePublic Sub DifferentDraw() Implements IShape.DrawEnd SubEnd ClassAccording to OOP rules any CCircle object *IS-A* IShape object and alsoIShape interface is a *CONTRACT* that guarantees all of its objectsimplement methods in this contract. But in the following test, obj2 which issupposed to obey above rules does not have access to Interface Draw()method!!!???Public Class TestPublic Sub Test()Dim obj1 As IShapeobj1 = New CCircleobj1.Draw()Dim obj2 As CCircleobj2 = New CCircleobj2.DifferentDraw()End SubEnd ClassThanksMasoud推荐答案我认为你需要将obj2转换为IShape才能直接访问 界面成员: DirectCast(obj2) ,IShape).Draw()I think you need to cast obj2 to IShape in order to directly access theinterface members:DirectCast(obj2,IShape).Draw() " Chris Dunaway" <杜****** @ gmail.com>在消息中写道 news:11 ********************* @ g49g2000cwa.googlegro ups.com ..."Chris Dunaway" <du******@gmail.com> wrote in messagenews:11*********************@g49g2000cwa.googlegro ups.com...我认为你需要将obj2转换为IShape才能直接访问接口成员: DirectCast(obj2,IShape).Draw() I think you need to cast obj2 to IShape in order to directly access the interface members: DirectCast(obj2,IShape).Draw() 它不是*必须*将派生类强制转换为层次结构中的基础来使用基本定义的方法。 Think类Object的ToString方法,每个类都有一个转换为 那个? 实际上它是OO语言的多态行为让我们使用 派生类和基本方法的访问。It is not a *have to* to cast a derived class to its base in hierarchy touse base defined methods.Think of ToString method of class Object, shall every class has a cast forthat?Actually it is polymorphic behavior of OO languages that let us use aderived class and access with the base methods.Masud 当你真的这么做时 obj2.DifferentDraw() 你正在调用IShape的Draw方法,但是你在Ccircle实现了 class。 调用接口方法是没用的,因为它们本身就是空的。 nyway。 HTH Irfan " masoud bayan" <毫安********** @ hotmail.com>在消息中写道 news:uO ************** @ TK2MSFTNGP14.phx.gbl ... MasudWhen you actually doobj2.DifferentDraw()You are calling the IShape ''s Draw method but implemented by you in Ccircleclass.It will be of no use to call Interface methods, because they themselves areempty anyway.HTHIrfan"masoud bayan" <ma**********@hotmail.com> wrote in messagenews:uO**************@TK2MSFTNGP14.phx.gbl...我来了在接口实现中我不确定在VB.NET中是否正确行为(也许是C#)? 请考虑以下示例: 子画面() 结束界面 公共类CCircle 实现IShape Public Sub DifferentDraw()实现IShape.Draw End Sub 结束类 根据OOP规则任何CCircle对象* IS-A * IShape对象以及 IShape接口是* CONTRACT *,保证其所有对象实现本合同中的方法。但是在下面的测试中,obj2 应该遵守以上规则,无法访问Interface Draw()方法!!! ??? 公开课测试 公共子测试() Dim obj1 as IShape obj1 =新CCircle obj1。画() 昏暗obj2作为CCircle obj2 =新CCircle obj2.DifferentDraw() 结束子 结束课 谢谢 Masoud I''ve come across something in Interface implementation that I am not sure is correct behavior in VB.NET (and maybe C#) or not? Consider following example: Public Interface IShape Sub Draw() End Interface Public Class CCircle Implements IShape Public Sub DifferentDraw() Implements IShape.Draw End Sub End Class According to OOP rules any CCircle object *IS-A* IShape object and also IShape interface is a *CONTRACT* that guarantees all of its objects implement methods in this contract. But in the following test, obj2 which is supposed to obey above rules does not have access to Interface Draw() method!!!??? Public Class Test Public Sub Test() Dim obj1 As IShape obj1 = New CCircle obj1.Draw() Dim obj2 As CCircle obj2 = New CCircle obj2.DifferentDraw() End Sub End Class Thanks Masoud 这篇关于界面奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 03:35
查看更多