gpt4 book ai didi

vb.net - 为什么接口(interface)不是强类型的?

转载 作者:行者123 更新时间:2023-12-01 17:20:35 26 4
gpt4 key购买 nike

我有以下代码编译没有问题。当然,在执行 Dim C As IDoThingsC = GetThing_C() 时,我遇到了无效的转换异常。我错过了什么吗?您是否想返回一个不满足函数返回值的接口(interface)要求的对象?

Public Class ClassA

Public Sub DoThings_A()
Debug.Print("Doing A things...")
End Sub

End Class


Public Class ClassB
Implements IDoThingsC

Public Sub DoThings_B()
Debug.Print("Doing B things...")
End Sub

Public Sub DoThings_C() Implements IDoThingsC.DoThings_C
Debug.Print("Doing C things...")
End Sub

End Class


Public Interface IDoThingsC

Sub DoThings_C()

End Interface


Public Class aTest

Public Sub Test()

Dim C As IDoThingsC = GetThing_C()
C.DoThings_C()

End Sub


Public Function GetThing_C() As IDoThingsC

Dim Thing As ClassA = New ClassA
Thing.DoThings_A()

Return Thing

End Function


End Class

最佳答案

使用Option Strict On位于源代码文件的顶部以捕获此类问题。您将收到编译时错误而不是运行时错误:

error BC30512: Option Strict On disallows implicit conversions from 'ClassA' to 'IDoThingsC'.

关于vb.net - 为什么接口(interface)不是强类型的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2030020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com