VB.NET模块的总结(二)

来源:互联网 发布:阿里云邮箱登陆官网 编辑:程序博客网 时间:2024/05/16 05:25

 

'Module1.vb文件中的代码:

Module MainClas
  
Sub Main()

    
Dim theFirstInstanceOfTestscope As New TestScope
    theFirstInstanceOfTestscope.TestScopeSubMethodOne()
    
Dim aStr As String = theFirstInstanceOfTestscope.TestScopeFunctiongMethodOne()
    Console.WriteLine(aStr)

    
Dim theFirstInstanceofOutTestScope As New OutTestScope()
    
'同上作用一样,实例化OutTestScope类
    'Dim theFirstInstanceOfOutTestScope As OutTestScope
    'theFirstInstanceOfOutTestScope = New OutTestScope()
    '同上作用一样,实例化OutTestScope类
    'Dim thefirstinstanceofouttestscope = New OutTestScope()
    theFirstInstanceofOutTestScope.OutTestScopeSubMethodOne()
    
Dim aaStr As String = theFirstInstanceofOutTestScope.OutTestScopeFunctiongMethodOne()
    Console.WriteLine(aaStr)
    Console.WriteLine(theFirstInstanceofOutTestScope.cc)

    theFirstInstanceOfTestscope.DiaoYongMMethod() 
'通过类的实例调用其他模块的方法
    Dim mTwoClassFiled As New mTwoClass
    mTwoClassFiled.mTwoClassMethod() 
'模块直接调用其他模块的类中的方法
    Console.WriteLine(mThreeMethod()) '模块直接调用其他模块的方法
    Console.ReadLine()

  
End Sub
End Module


Class TestScope
  
Dim a As String
  
Private b As String
  
Public c As String
  
Sub New() '无参数构造函数
    Me.a = "Dim_a"
    
Me.b = "Private_b"
    
Me.c = "Public_c"
  
End Sub
  
'Public Static Sub TestScopeMethodOne()  '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
  Public Sub TestScopeSubMethodOne()
    Console.WriteLine(
String.Format("TestScopeSubMethodOne():{0} {1} {2}", a, b, c))
  
End Sub
  
'Public Static Function TestScopeFunctiongMethodOne() As String  ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
  Public Function TestScopeFunctiongMethodOne() As String
    
Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c)
  
End Function
  
Public Sub DiaoYongMMethod()
    
'mTwoMethod()
    Console.WriteLine(mThreeMethod())
  
End Sub
End Class

Module mTwo
  
'Public Sub mTwoMethod()
  Private mTwoFiled As String = "mTwoFiled"
  
Private Sub mTwoMethod()  '模块中的方法可以用Private修饰,但只能在当前模块中使用,不能在其他模块中使用
    Console.WriteLine(mTwoFiled)
  
End Sub
  
Class mTwoClass
    
Sub mTwoClassMethod()
      mTwoMethod()
    
End Sub
  
End Class
End Module

Module mThree
  
Private Sub mPrivateThreeMethod()
    Console.WriteLine(
"mPrivateThreeMethod()")
  
End Sub
  
'Public Function mThreeMethod() As String
  Function mThreeMethod() As String '模块中的方法隐士共享,可以在模块外部访问,加不加Public都一样
    mPrivateThreeMethod()
    
Return "mThreeMethod()"
  
End Function
End Module

'在vb.net中不能用static来声明方法(Sub和Function),并且不能用来声明成员变量,只能用来声明方法(Sub和Function)中的静态变量。   
'
shared既可以用来声明变量也可以用来声明方法,还可以用来声明成员变量,这一点刚好跟static相反。
'
vb.ne中的shared更像C#中static的作用。在vb.net中用shared声明的成员变量(字段)和方法(Sub和Function),只能用类名来访问,而不能用类的实例来访问

'一个文件中可以定义多个模块
'
可以像C#一样在另一个文件中定义一类,该类的用法和在一个文件中定义的类的用法相同(包裹字段,方法,属性等等都一样)

'模块中的方法隐士共享,可以在模块外部(其他模块,类,结构等)被直接访问,加不加Public都一样
'
模块中的方法不能声明为Protected,Shared和Static,只能声明为Public(默认就是Public)和Private。
'
但模块中类(或者结构)的方法可以声明为Public,Private,Protected和Shared。



'OutTestScope.vb文件中的代码:

Public Class OutTestScope
  
Dim aa As String
  
Private bb As String
  
Public cc As String
  
Sub New() '无参数构造函数
    Me.aa = "Dim_aa"
    
Me.bb = "Private_bb"
    
Me.cc = "Public_cc"
  
End Sub
  
'Public Static Sub TestScopeMethodOne()  '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
  Public Sub OutTestScopeSubMethodOne()
    Console.WriteLine(
String.Format("OutTestScopeSubMethodOne():{0} {1} {2}", aa, bb, cc))
  
End Sub
  
'Public Static Function TestScopeFunctiongMethodOne() As String  ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
  Public Function OutTestScopeFunctiongMethodOne() As String
    
Return String.Format("OutTestScopeFunctiongMethodOne():{0} {1} {2}", aa, bb, cc)
  
End Function
End Class

原创粉丝点击