简体   繁体   English

VB.net无法继承

[英]VB.net cannot inherit

I am fairly new to VB.net, I cannot compile this code, and I don't get why. 我是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

Error 2 'Triangulo' cannot inherit from class 'Poligono' because it expands the access of the base class outside the assembly. 错误2'Triangulo'不能从类'Poligono'继承,因为它扩展了程序集外部基类的访问。 c:\\users\\win7\\documents\\visual studio 2013\\Projects\\WindowsApplication1\\WindowsApplication1\\Form1.vb 33 14 WindowsApplication1 c:\\ users \\ win7 \\ documents \\ visual studio 2013 \\ Projects \\ WindowsApplication1 \\ WindowsApplication1 \\ Form1.vb 33 14 WindowsApplication1

Thanks! 谢谢!

By default, classes declared at the namespace level get Friend access level . 默认情况下, 在命名空间级别声明的类将获得Friend访问级别 So Poligono is Friend Class . 所以PoligonoFriend Class A Friend class may not be made visible to general public. 一般公众可能无法看到Friend类。

Change 更改

MustInherit Class Poligono

to

Public MustInherit Class Poligono

Two things: 两件事情:

  1. You have to add the return type with As 您必须使用As添加返回类型

     Public MustOverride Function obtenerPerimetro() As Object Public MustOverride Function cargarLados() As Object 
  2. You have to add Overrides to the method that overrides the abstract base class 您必须将Overrides添加到覆盖抽象基类的方法中

     Public Overrides Function cargarLados() As Object Return 1 End Function Public Overrides Function obtenerPerimetro() As Object Return 1 End Function 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM