本文向大家介绍VB.NET静态成员,可能好多人还不了解VB.NET静态成员,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
VB.NET静态成员
在面向对象编程语言中,有一种被称作静态成员的特殊的类成员,VB.NET也有静态成员这一概念。无需对一个对象实例化就可以使用其中的VB.NET静态成员。例如,下面的表中,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
我们可以像下表中的程序所演示的那样在程序中使用类:
- 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。不属于VB.NET静态成员的类成员被称作实例成员。
【编辑推荐】