【问题标题】:Call Base functions from Child in VB.net在 VB.net 中从 Child 调用 Base 函数
【发布时间】:2015-08-29 06:54:35
【问题描述】:

如何调用vb.net中的基函数?

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Box
    Public length As Double   ' Length of a box
    Public breadth As Double   ' Breadth of a box
    Public height As Double    ' Height of a box
    Public function setLength(ByVal len As Double)
        length = len
    End Sub
    Public Sub setBreadth(ByVal bre As Double)
        breadth = bre
    End Sub
    Public Sub setHeight(ByVal hei As Double)
        height = hei
    End Sub
    Public Function getVolume() As Double
        Return length * breadth * height
     End Function
 End Class

使用 MyBase 调用基函数时出现语法错误

Public Class myChild : Inherits Box
    'box 1 specification
    MyBase.setLength(6.0)
    MyBase.setBreadth(7.0)
    MyBase.setHeight(5.0)

    'box 2 specification
    MyBase.setLength(12.0)
    MyBase.setBreadth(13.0)
    MyBase.setHeight(10.0) 

    'volume of box 1
    volume = MyBase.getVolume()
    Console.WriteLine("Volume of Box1 : {0}", volume)

    'volume of box 2
    volume = MyBase.getVolume()
End Class

【问题讨论】:

    标签: vb.net-2010


    【解决方案1】:

    您不能从那里调用MyBase,因为该对象尚未构建。

    更好的实现是:

    Box.vb

    Public Class Box
        Private mLength As Double   ' Length of a box
        Private mBreadth As Double   ' Breadth of a box
        Private mHeight As Double    ' Height of a box
    
        Public Sub New(ByVal length As Double, ByVal breadth As Double, ByVal height As Double)
            Me.mLength = length
            Me.mBreadth = breadth
            Me.mHeight = height
    
        End Sub
        Public Property Length As Double
            Get
                Return Me.mLength
            End Get
            Set(ByVal value As Double)
                Me.mLength = value
            End Set
        End Property
    
    Public Property Breadth As Double
        Get
            Return Me.mBreadth
        End Get
        Set(ByVal value As Double)
            Me.mBreadth = value
        End Set
    End Property
    
    Public Property Height As Double
        Get
            Return Me.mHeight
        End Get
        Set(ByVal value As Double)
            Me.mHeight = value
        End Set
    End Property
    
    Public Function getVolume() As Double
        Return Length * Breadth * Height
    End Function
    End Class
    

    Child.vb

    Public Class Child : Inherits Box
    
        Public Sub New(ByVal length As Double, ByVal breadth As Double, ByVal height As Double)
            MyBase.New(length, breadth, height)
        End Sub
    
    End Class
    

    例子

     Sub Main()
            Dim box1 As New Child(6.0, 7.0, 5.0)
            Dim box2 As New Child(12.0, 13.0, 10.0)
    
            Console.WriteLine("box1 volume is: {0}", box1.getVolume())
            Console.WriteLine("box2 volume is: {0}", box2.getVolume())
        End Sub
    

    【讨论】:

    • 嗯。所以在vb.net中,不需要使用include/require函数来使用其他文件吗?就像 php 使用 include/require 函数一样。
    • @J.Ignacio 如果所有文件都在同一个命名空间下,那么您不需要显式导入文件。我的 VB.NET 非常粗糙,但我认为如果文件位于不同的命名空间中,您将需要导入 - 尽管 VB 对大多数事情都非常宽容。