VB.Net/Class/Shared
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Console.WriteLine("Students before instantiation: " & Student.Count)
Dim student1 As Student = New Student("A", "B")
Dim student2 As Student = New Student("C", "D")
" output of student2 after instantiation
Console.WriteLine("Students after instantiation: " & vbCrLf & _
"via Student.Count: " & Student.Count)
" display name of first and second student
Console.WriteLine(vbCrLf & "Students 1: " & _
student1.FirstName & " " & student1.LastName & _
vbCrLf & "Student 2: " & student2.FirstName & " " & _
student2.LastName)
" mark student1 and student2 for garbage collection
student1 = Nothing
student2 = Nothing
System.GC.Collect() " request garbage collection
End Sub
End Class
" Class Student uses Shared variable.
Class Student
Inherits Object
Private mFirstName As String
Private mLastName As String
" number of objects in memory
Private Shared mCount As Integer
" Student constructor
Public Sub New(ByVal firstNameValue As String, _
ByVal lastNameValue As String)
mFirstName = firstNameValue
mLastName = lastNameValue
mCount += 1 " increment shared count of students
Console.WriteLine _
("Student object constructor: " & mFirstName & _
" " & mLastName)
End Sub " New
" finalizer method decrements Shared count of students
Protected Overrides Sub Finalize()
mCount -= 1 " decrement mCount, resulting in one fewer object
Console.WriteLine _
("Student object finalizer: " & mFirstName & _
" " & mLastName & "; count = " & mCount)
End Sub " Finalize
" return first name
Public ReadOnly Property FirstName() As String
Get
Return mFirstName
End Get
End Property " FirstName
" return last name
Public ReadOnly Property LastName() As String
Get
Return mLastName
End Get
End Property " LastName
" property Count
Public Shared ReadOnly Property Count() As Integer
Get
Return mCount
End Get
End Property " Count
End Class " Student
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Today: " & Today.TodayDate)
Console.WriteLine("Day: " & Today.Day)
Console.WriteLine("Month: " & Today.Month)
Console.WriteLine("Year: " & Today.Year)
Console.WriteLine("Day of week: " & Today.NumericDayOfWeek)
Console.WriteLine("Day of week: " & Today.StringDayOfWeek)
End Sub
End Module
Class Today
Public Shared TodayDate As DateTime = Now()
Public Shared Day As Integer = Now.Day()
Public Shared Month As Integer = Now.Month()
Public Shared Year As Integer = Now.Year()
Public Shared NumericDayOfWeek As Integer = Now.DayOfWeek()
Public Shared StringDayOfWeek As String = Now.DayOfWeek.ToString()
End Class
Imports System
public class MainClass
Shared Sub Main()
Dim c1 As New C()
Dim c2 As New C()
c2.Test()
c.SharedTest()
End Sub
End Class
Class C
Public Shared Sub SharedTest()
Static x As Integer
x = x + 1
console.WriteLine(x)
End Sub
Public Sub Test()
Static x As Integer
x = x + 1
console.WriteLine(x)
End Sub
End Class
Imports System.IO
Module Module1
Sub Main()
Dim p As New Professor()
Console.WriteLine("count {0}", p.Count)
Dim pp As New Professor()
Console.WriteLine("count {0}", pp.Count)
End Sub
End Module
Class Professor
Public Shared Count As Integer = 0
Public Name As String
Private Age As Integer
Public OfficeNumber As String
Private HomePhone As String
Public Sub New()
Count = Count + 1
End Sub
End Class
Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main()
Dim newUser As User
newUser = User.CreateUser("Joe", "password15")
Console.WriteLine(newUser.Username)
End Sub
End Class
Public Class User
Public Username As String
Private password As String
Public Shared Function CreateUser(ByVal username As String, _
ByVal password As String) As User
Dim user As New User()
user.Username = username
user.Password = password
Return user
End Function
End Class