VB.net无法继承

时间:2013-11-04 21:05:41

标签: vb.net

我是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

谢谢!

2 个答案:

答案 0 :(得分:7)

默认情况下,classes declared at the namespace level get Friend access level。因此PoligonoFriend Class。一般公众可能无法看到Friend课程。

更改

MustInherit Class Poligono

Public MustInherit Class Poligono

答案 1 :(得分: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