Module Module1
Sub Main()
Dim theFirstInstanceOfTestscope As New TestScope
theFirstInstanceOfTestscope.TestScopeSubMethodOne()
'TestScope.TestScopeSubMethodOne()
Dim aStr As String = theFirstInstanceOfTestscope.TestScopeFunctiongMethodOne()
Console.WriteLine(aStr)
Console.ReadLine()
End Sub
Dim theFirstInstanceOfTestscope As New TestScope
theFirstInstanceOfTestscope.TestScopeSubMethodOne()
'TestScope.TestScopeSubMethodOne()
Dim aStr As String = theFirstInstanceOfTestscope.TestScopeFunctiongMethodOne()
Console.WriteLine(aStr)
Console.ReadLine()
End Sub
Class TestScope
Dim a As String '同Private a As String作用一样。 类的字段(又称成员变量)默认是Private和实例类型
Private b As String
Public c As String
Dim a As String '同Private a As String作用一样。 类的字段(又称成员变量)默认是Private和实例类型
Private b As String
Public c As String
Private Shared d As String
Sub New() '无参数构造函数
Me.a = "Dim_a"
Me.b = "Private_b"
Me.c = "Public_c"
TestScope.d = "Private Shared_d"
End Sub
'Public Static Sub TestScopeMethodOne() '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
Public Sub TestScopeSubMethodOne() '如果改成Private,就不能在类的外部访问
Console.WriteLine(String.Format("TestScopeSubMethodOne():{0} {1} {2}", a, b, c))
Console.WriteLine(TestScope.d) '实例方法能访问Shared成员(其实就是静态成员)
Dim TestScopeFiled As New TestScope
TestScopeSharedSubMethodOne(TestScopeFiled)
End Sub
'Public Static Function TestScopeFunctiongMethodOne() As String ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
Public Function TestScopeFunctiongMethodOne() As String '如果改成Private,就不能在类的外部访问
Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c)
End Function
Me.a = "Dim_a"
Me.b = "Private_b"
Me.c = "Public_c"
TestScope.d = "Private Shared_d"
End Sub
'Public Static Sub TestScopeMethodOne() '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
Public Sub TestScopeSubMethodOne() '如果改成Private,就不能在类的外部访问
Console.WriteLine(String.Format("TestScopeSubMethodOne():{0} {1} {2}", a, b, c))
Console.WriteLine(TestScope.d) '实例方法能访问Shared成员(其实就是静态成员)
Dim TestScopeFiled As New TestScope
TestScopeSharedSubMethodOne(TestScopeFiled)
End Sub
'Public Static Function TestScopeFunctiongMethodOne() As String ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
Public Function TestScopeFunctiongMethodOne() As String '如果改成Private,就不能在类的外部访问
Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c)
End Function
Public Shared Sub TestScopeSharedSubMethodOne(ByVal filedOne As TestScope)
'Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c) '错误: 没有类的显式实例,就无法从共享方法或共享成员初始值设定项中引用该类的实例成员
'这个错误和C#其实是一样的,C#允许实例方法可以访问静态方法和静态字段,但C#不允许类和结构里的静态方法去访问同一个类或结构里的实例方法和实例字段,VB.NET也遵从这个规则。
filedOne.a = "用传参数的形式调用TestScope类的实例字段" '但可以用该类的实例做为方法的参数来调用实例字段和实例方法
Dim sharedMethodFiled As String = filedOne.TestScopeFunctiongMethodOne() '但可以用该类的实例做为方法的参数来调用实例字段和实例方法
Console.WriteLine("{0}\{1}", filedOne.a, sharedMethodFiled)
'fileOne.TestScopeSubMethodOne()
Console.WriteLine("TestScopeFunctiongSharedMethodOne")
'Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c) '错误: 没有类的显式实例,就无法从共享方法或共享成员初始值设定项中引用该类的实例成员
'这个错误和C#其实是一样的,C#允许实例方法可以访问静态方法和静态字段,但C#不允许类和结构里的静态方法去访问同一个类或结构里的实例方法和实例字段,VB.NET也遵从这个规则。
filedOne.a = "用传参数的形式调用TestScope类的实例字段" '但可以用该类的实例做为方法的参数来调用实例字段和实例方法
Dim sharedMethodFiled As String = filedOne.TestScopeFunctiongMethodOne() '但可以用该类的实例做为方法的参数来调用实例字段和实例方法
Console.WriteLine("{0}\{1}", filedOne.a, sharedMethodFiled)
'fileOne.TestScopeSubMethodOne()
Console.WriteLine("TestScopeFunctiongSharedMethodOne")
Dim anotherFiled As New TestScope '还可以通过在静态方法中实例化该类来调用该类的实例字段
Console.WriteLine(anotherFiled) '这个方法会用重载的ToString方法来打印该对象
Console.WriteLine(anotherFiled) '这个方法会用重载的ToString方法来打印该对象
End Sub
Public Overrides Function ToString() As String
'Return MyBase.ToString()
Return String.Format("{0}::{1}::{2}", a, b, c)
End Function
End Class
Public Overrides Function ToString() As String
'Return MyBase.ToString()
Return String.Format("{0}::{1}::{2}", a, b, c)
End Function
End Class
End Module
''''''打印如下--------------
'TestScopeSubMethodOne():Dim_a Private_b Public_c
'Private Shared_d
'用传参数的形式调用TestScope类的实例字段\TestScopeFunctiongMethodOne():用传参数的形式调用TestScope类的实例字段 Private_b Public_c
'TestScopeFunctiongSharedMethodOne
'Dim_a::Private_b::Public_c
'TestScopeFunctiongMethodOne():Dim_a Private_b Public_c
''''''打印如下--------------
'TestScopeSubMethodOne():Dim_a Private_b Public_c
'Private Shared_d
'用传参数的形式调用TestScope类的实例字段\TestScopeFunctiongMethodOne():用传参数的形式调用TestScope类的实例字段 Private_b Public_c
'TestScopeFunctiongSharedMethodOne
'Dim_a::Private_b::Public_c
'TestScopeFunctiongMethodOne():Dim_a Private_b Public_c
'''''----------
'一点总结"
'在vb.net中不能用static来声明方法(Sub和Function),并且不能用来声明成员变量,只能用来声明方法(Sub和Function)中的静态变量。
'shared既可以用来声明变量也可以用来声明方法,还可以用来声明成员变量,这一点刚好跟static相反。
'vb.ne中的shared更像C#中static的作用。在vb.net中用shared声明的成员变量(字段)和方法(Sub和Function),只能用类名来访问,而不能用类的实例来访问
'无限循环和无限递归会导致:未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中
本文转自terryli51CTO博客,原文链接: http://blog.51cto.com/terryli/520601,如需转载请自行联系原作者