且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

界面奇怪的行为

更新时间:2022-04-30 22:34:55

我认为你需要将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()




" Chris Dunaway" <杜****** @ gmail.com>在消息中写道

news:11 ********************* @ g49g2000cwa.googlegro ups.com ...

"Chris Dunaway" <du******@gmail.com> wrote in message
news: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 to
use base defined methods.

Think of ToString method of class Object, shall every class has a cast for
that?

Actually it is polymorphic behavior of OO languages that let us use a
derived class and access with the base methods.



Masud


当你真的这么做时

obj2.DifferentDraw()

你正在调用IShape的Draw方法,但是你在Ccircle实现了

class。

调用接口方法是没用的,因为它们本身就是空的。 nyway。


HTH

Irfan


" masoud bayan" &LT;毫安********** @ hotmail.com&GT;在消息中写道

news:uO ************** @ TK2MSFTNGP14.phx.gbl ...
Masud

When you actually do
obj2.DifferentDraw()
You are calling the IShape ''s Draw method but implemented by you in Ccircle
class.
It will be of no use to call Interface methods, because they themselves are
empty anyway.

HTH
Irfan

"masoud bayan" <ma**********@hotmail.com> wrote in message
news: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