Write有点不理解,为什么我们可以这样做呢?因为在面向对象编程语言中,有一种被称作静态成员的特殊的类成员,VB.NET也有静态成员这一概念。
无需对一个对象实例化就可以使用其中的静态成员。例如,下面的表7中,SalaryLevel类中就只包含有静态的域:
类中的表态成员
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
我们可以象表8中的程序所演示的那样在程序中使用类:
Listing 8: Using a static member of a class
Imports System
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
Class Employee
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary()
''使用SalaryLevel类的静态域向Console输出工资额
Console.Write(SalaryLevel.Level4)
End Sub
Public Shared Sub Main()
Dim employee As Employee
employee = New Employee()
employee.PrintSalary()
End Sub
End Class
在Employee类的PrintSalary方法中,我们可以在不首先创建SalaryLevel类变量的情况下使用其中的静态域Level4。不属于静态成员的类成员被称作实例成员。
构造器
A构造器是类初始化所必须使用的特殊方法,在VB.NET中,这一方法被称作New。但我们在前面的代码中可以发现,在类中我们没有定义New这个方法。正是这样,如果类中没有定义构造器,VB.NET将