vb.net操作符 vbnet implements( 三 )


Public可以从class外访问
Friend限于class所属的应用程序内
Protected只能被class和其派生类访问
Protected Friend能被class、应用程序和派生类访问
继承继承是面向对象编程语言中最常用的技术 。继承让你能够重用类代码和功能 。
VB.NET支持继承,而VB6.0则不支持 。继承的好处在于你能使用任何人编写的类 , 从这些类派生自己的类,然后在自己的类中调用父类功能 。在下面的例子中,Class B派生自Class A,我们将从Class B中调用Class A的方法MethodA 。
Imports System
Imports System.Console
Module Module1
Sub Main()
Dim bObj As B = New B
WriteLine(bObj.MethodA())
End Sub
End Module
' Class A defined
Public Class A
Function MethodA() As String
Return "Method A is called."
End Function
End Class
'Class B, inherited from Class A. All members (Public and Protected)
' can be access via B now.
Public Class B
Inherits A
Function MethodB() As String
Return "Method B is called."
End Function
End Class
可以从一个class中派生多个自定义class , 也可以从多个class派生一个自定义class 。
共享的成员类的共享成员被类的所有实体共享 。共享成员可能是属性、方法或字段/值域 。在你不想让用户全面控制自己的类时,共享成员相当有用 。例如 , 你可以开发一个类库,让用户通过共享成员使用其中的部分功能 。
可以通过class本身引用共享成员 , 而无需通过类的实体 。例如:Module Module1
Sub Main()
WriteLine(A.MethodA())
End Sub
End Module
' Class A defined
Public Class A
Shared Function MethodA() As String
Return "Method A is called."
End Function
End Class
多线程VB语言的一大弱点就是缺乏编写自由线程(free-threaded)程序的能力 。在.NET Framework中,所有语言共享CRL(Common Runtime Library,公共运行库),也就是说,你可以用VB.NET、C#或其它.NET语言编写同样的程序 。
System.Threading namespace定义了线程类 。我们只需要引入System.Threading namespace,即可使用线程类 。
System.Threading.Thread类提供线程对象,可以使用Thread类创建或破坏线程 。
创建线程使用Thread类的实体创建一个新线程,然后用Thread.Start方法开始执行线程 。线程构造器接受一个参数 , 该参数指明你要在线程中执行的procedure 。在下例中,我想在oThread1(Thread类的一个实体)的第二线程中执行SecondThread过程:
oThread1 = New Thread(AddressOf SecondThread)
SecondThread procedure looks like below:
Public Sub SecondThread()
Dim i As Integer
For i = 1 To 10
Console.WriteLine(i.ToString())
Next
End Sub
然后,调用Thread.Start()开始线程:
oThread1.Start()
下面的代码创建两个第二线程:
Imports System
Imports System.Threading
Module Module1
Public oThread1 As Thread
Public oThread2 As Thread
Sub Main()
oThread1 = New Thread(AddressOf SecondThread)
oThread2 = New Thread(AddressOf ThirdThread)
oThread1.Start()
oThread2.Start()
End Sub
Public Sub SecondThread()
Dim i As Integer
For i = 1 To 10
Console.WriteLine(i.ToString())
Next
End Sub
Public Sub ThirdThread()
Dim i As Integer
For i = 1 To 10
Console.WriteLine("A" + i.ToString())
Next
End Sub
End Module
破坏线程 调用Abort方法来破坏(中止)一个线程 。在调用Abort之前 , 确保用IsAlive判断线程处于活动状态 。
If oThread1.IsAlive Then
oThread1.Abort()

推荐阅读