3

我对 VB.net 很陌生,我无法编译这段代码,我不明白为什么。

MustInherit Class Poligono

    Protected p_cant_Lados As Integer
    Public Property cant_Lados() As Integer
        Get
            Return p_cant_Lados
        End Get
        Set(ByVal value As Integer)
            p_cant_Lados = value
        End Set
    End Property

    Public MustOverride Function obtenerPerimetro()
    Public MustOverride Function cargarLados()

End Class

Public Class Triangulo
    Inherits Poligono

    Private lado1 As Integer
    Private lado2 As Integer
    Private lado3 As Integer

    Public Function cargarLados() As Object
        Return 1
    End Function

    Public Function obtenerPerimetro() As Object
        Return 1
    End Function

End Class

错误 2 'Triangulo' 不能从类 'Poligono' 继承,因为它扩展了程序集外部基类的访问权限。c:\users\win7\documents\visual studio 2013\Projects\WindowsApplication1\WindowsApplication1\Form1.vb 33 14 WindowsApplication1

谢谢!

4

2 回答 2

8

默认情况下,在命名空间级别声明的类获得Friend访问级别Poligono也是如此Friend Class。一个Friend类可能对公众不可见。

改变

MustInherit Class Poligono

Public MustInherit Class Poligono
于 2013-11-04T21:07:45.803 回答
3

两件事情:

  1. 您必须添加返回类型As

    Public MustOverride Function obtenerPerimetro() As Object
    Public MustOverride Function cargarLados() As Object
    
  2. 您必须添加Overrides到覆盖抽象基类的方法

    Public Overrides Function cargarLados() As Object
        Return 1
    End Function
    
    Public Overrides Function obtenerPerimetro() As Object
        Return 1
    End Function
    
于 2013-11-04T21:10:43.117 回答