VB.NET经过长时间的发展,很多用户都很了解VB.NET名字空间了,这里我发表一下个人理解,和大家讨论讨论。当写.NET应用程序时,需要写类和其他数据类型。为使应用程序更有条理,组织性更好,需要将他们聚合进名字空间中,这也是微软用.NET Framework类库的原因。
#T#微软.NET Framework sdk文档中的.NET Framework类库中包含了80多个名字空间,包括常用的重要的名字空间,如System,System.IO,System.Drawing,System.Windows.Forms等等。举例而言,在Employee类中的PrintSalary 方法,我们使用了system名字空间中的console类。
如果在程序中要经常使用一个VB.NET名字空间,可以采取引用该名字空间的方法,这样在每次调用其成员时就用不作重复写VB.NET名字空间了。例如你可以象下面这样改写list4和list5。
Imports System
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary()
' print the salary to the Console
Console.Write(salary)
End Sub
Public Shared Sub Main()
Dim employee As Employee
employee = New Employee()
employee.PrintSalary()
End Sub
End Class
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
现在你可以在PrintSalary方法中使用名字空间而不用提及名字空间,因为名字空间已经引用了。
在不同的VB.NET名字空间允许有相同名字的类,正确地引用一个类最普通的实践是提到过的在类名前面的名字空间。如system名字空间中的Console的引用方法是:System.Console。