VB.Net/Class/Constructor
Содержание
Call base constructor
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim w As New Window(5, 10) w.DrawWindow( ) Dim lb As New ListBox(20, 30, "Hello world") lb.DrawWindow( ) End Sub
End Class
Public Class Window Public Sub New(ByVal top As Integer, ByVal left As Integer) Me.top = top Me.left = left End Sub "New Public Sub DrawWindow( ) Console.WriteLine("Drawing Window at {0}, {1}", top, left) End Sub Private top As Integer Private left As Integer End Class Public Class ListBox Inherits Window Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal theContents As String) MyBase.New(top, left) " mListBoxContents = theContents End Sub Public Shadows Sub DrawWindow( ) MyBase.DrawWindow( ) Console.WriteLine("Writing string to the listbox: {0}", mListBoxContents) End Sub Private mListBoxContents As String End Class </source>
Class with two constructors
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim currentTime As DateTime = DateTime.Now Dim time1 As New Time(currentTime) time1.DisplayCurrentTime( ) Dim theHour As Integer = time1.Hour Console.WriteLine("Retrieved the hour: {0}", _ theHour) theHour += 1 time1.Hour = theHour Console.WriteLine("Updated the hour: {0}", _ time1.Hour) End Sub
End Class
Public Class Time Private mYear As Integer Private mMonth As Integer Private mDayOfMonth As Integer Private mHour As Integer Private mMinute As Integer Private mSecond As Integer Property Hour( ) As Integer Get Return mHour End Get Set(ByVal Value As Integer) mHour = Value End Set End Property
" public accessor methods Public Sub DisplayCurrentTime( ) Console.WriteLine( _ "{0}/{1}/{2} {3}:{4}:{5}", _ mMonth, mDayOfMonth, mYear, Hour, mMinute, mSecond) End Sub "DisplayCurrentTime
" constructors Public Sub New(ByVal dt As DateTime) mYear = dt.Year mMonth = dt.Month mDayOfMonth = dt.Day mHour = dt.Hour mMinute = dt.Minute mSecond = dt.Second End Sub "New
Public Sub New( _ ByVal mYear As Integer, _ ByVal mMonth As Integer, _ ByVal mDayOfMonth As Integer, _ ByVal mHour As Integer, _ ByVal mMinute As Integer, _ ByVal mSecond As Integer) Me.mYear = mYear Me.mMonth = mMonth Me.mDayOfMonth = mDayOfMonth Me.Hour = mHour Me.mMinute = mMinute Me.mSecond = mSecond End Sub "New End Class "Time </source>
Constructor with parameters
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim currentTime As DateTime = DateTime.Now Dim time1 As New Time(currentTime) time1.DisplayCurrentTime( ) Dim time2 As New Time(2005, 11, 18, 11, 3, 30) time2.DisplayCurrentTime( ) End Sub
End Class
Public Class Time Private year As Integer Private month As Integer Private dayOfMonth As Integer Private hour As Integer Private minute As Integer Private second As Integer
Public Sub DisplayCurrentTime( ) Console.WriteLine( _ "{0}/{1}/{2} {3}:{4}:{5}", _ month, dayOfMonth, year, hour, minute, second) End Sub Public Sub New(ByVal dt As DateTime) year = dt.Year month = dt.Month dayOfMonth = dt.Day hour = dt.Hour minute = dt.Minute second = dt.Second End Sub Public Sub New( _ ByVal year As Integer, _ ByVal month As Integer, _ ByVal dayOfMonth As Integer, _ ByVal hour As Integer, _ ByVal minute As Integer, _ ByVal second As Integer) Me.year = year Me.month = month Me.dayOfMonth = dayOfMonth Me.hour = hour Me.minute = minute Me.second = second End Sub End Class </source>
Init Member Variables through Constructor
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim timeObject As New Time(2005, 3, 25, 9, 35, 20) timeObject.DisplayCurrentTime( ) End Sub
End Class
Public Class Time Private Year As Integer Private Month As Integer Private Day As Integer Private Hour As Integer Private Minute As Integer Private Second As Integer " Public methods Public Sub DisplayCurrentTime( ) System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", _ Month, Day, Year, Hour, Minute, Second) End Sub "DisplayCurrentTime " Constructor Public Sub New(ByVal theYear As Integer, ByVal theMonth As Integer, _ ByVal theDate As Integer, ByVal theHour As Integer, _ ByVal theMinute As Integer, ByVal theSecond As Integer) Year = theYear Month = theMonth Day = theDate Hour = theHour Minute = theMinute Second = theSecond End Sub End Class "Time </source>
Multiplie Constructors
<source lang="vbnet"> Imports System.IO Module Module1
Sub Main() Dim Palm = New Book("Book 1", 49.99) Dim CSharp = New Book("Book 2", "Name", "Publisher", 49.99) Dim VB = New Book("Book 3", "Name 2", "Publish", 49.99, 18, "April 2002") End Sub
End Module
Class Book Private Title As String Private Publisher As String Private Author As String Private Price As Double Private ChapterCount As Integer Private CopyrightDate As String Public Sub New(ByVal Title As String, ByVal Price As Double) Me.Title = Title Me.Price = Price Me.Author = "" Me.ChapterCount = 0 Me.Publisher = "" Me.CopyrightDate = "" End Sub Public Sub New(ByVal Title As String, ByVal Author As String, ByVal Publisher As String, ByVal Price As Double) Me.Title = Title Me.Author = Author Me.Publisher = Publisher Me.Price = Price Me.ChapterCount = 0 Me.CopyrightDate = "" End Sub Public Sub New(ByVal Title As String, ByVal Author As String, ByVal Publisher As String, ByVal Price As Double, ByVal ChapterCount As Integer, ByVal CopyrightDate As Date) Me.Title = Title Me.Author = Author Me.Publisher = Publisher Me.Price = Price Me.ChapterCount = ChapterCount Me.CopyrightDate = CopyrightDate End Sub End Class </source>
Overloading constructors
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) " use overloaded constructors Dim time1 As New CTime2() Dim time2 As New CTime2(2) Dim time3 As New CTime2(21, 34) Dim time4 As New CTime2(12, 25, 42) Dim time5 As New CTime2(27, 74, 99) Dim time6 As New CTime2(time4) " use time4 as initial value Const SPACING As Integer = 13 " spacing between output text Console.WriteLine( "Constructed with: " & vbCrLf & _ " time1: all arguments defaulted" & vbCrLf & _ Space(SPACING) & time1.ToUniversalString() ) " invoke time2 methods Console.WriteLine( " time2: hour specified; minute and second defaulted" & _ vbCrLf & Space(SPACING) & _ time2.ToUniversalString() ) " invoke time3 methods Console.WriteLine( " time3: hour and minute specified; second defaulted" & _ vbCrLf & Space(SPACING) & time3.ToUniversalString() ) " invoke time4 methods Console.WriteLine( " time4: hour, minute and second specified" & _ vbCrLf & Space(SPACING) & time4.ToUniversalString() ) " invoke time5 methods Console.WriteLine( " time5: hour, minute and second specified" & _ vbCrLf & Space(SPACING) & time5.ToUniversalString() ) " invoke time6 methods Console.WriteLine( " time6: Time2 object time4 specified" & vbCrLf & _ Space(SPACING) & time6.ToUniversalString() )
End Sub
End Class
" Represents time and contains overloaded constructors. Class CTime2
Inherits Object Private mHour As Integer " 0 - 23 Private mMinute As Integer " 0 - 59 Private mSecond As Integer " 0 - 59 " constructor initializes each variable to zero and " ensures that each CTime2 object starts in consistent state Public Sub New() SetTime() End Sub " New " CTime2 constructor: hour supplied; " minute and second default to 0 Public Sub New(ByVal hourValue As Integer) SetTime(hourValue) End Sub " New " CTime2 constructor: hour and minute supplied; " second defaulted to 0 Public Sub New(ByVal hourValue As Integer, _ ByVal minuteValue As Integer) SetTime(hourValue, minuteValue) End Sub " New " CTime2 constructor: hour, minute and second supplied Public Sub New(ByVal hourValue As Integer, _ ByVal minuteValue As Integer, ByVal secondValue As Integer) SetTime(hourValue, minuteValue, secondValue) End Sub " New " CTime2 constructor: another CTime2 object supplied Public Sub New(ByVal timeValue As CTime2) SetTime(timeValue.mHour, timeValue.mMinute, timeValue.mSecond) End Sub " New " set new time value using universal time; " perform validity checks on data; " set invalid values to zero Public Sub SetTime(Optional ByVal hourValue As Integer = 0, _ Optional ByVal minuteValue As Integer = 0, _ Optional ByVal secondValue As Integer = 0) " perform validity checks on hour, then set hour If (hourValue >= 0 AndAlso hourValue < 24) Then mHour = hourValue Else mHour = 0 End If " perform validity checks on minute, then set minute If (minuteValue >= 0 AndAlso minuteValue < 60) Then mMinute = minuteValue Else mMinute = 0 End If " perform validity checks on second, then set second If (secondValue >= 0 AndAlso secondValue < 60) Then mSecond = secondValue Else mSecond = 0 End If End Sub " SetTime " convert String to universal-time format Public Function ToUniversalString() As String Return String.Format("{0}:{1:D2}:{2:D2}", _ mHour, mMinute, mSecond) End Function " ToUniversalString
End Class
</source>
Use Constructor to init member variables
<source lang="vbnet"> Imports System Imports System.IO
Public Class MainClass
Shared Sub Main() Dim e As New Employee("Joe", 10000) Console.WriteLine(e.TheName & " salary is " & e.Salary) Console.WriteLine(e) End Sub Public Class Employee Private m_Name As String Private m_Salary As Decimal Public Sub New(ByVal sName As String, ByVal curSalary As Decimal) If sName = String.Empty Then Console.WriteLine("no names") Else m_Name = sName End If End Sub Public Property TheName() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property Public ReadOnly Property Salary() As Decimal Get Return m_Salary End Get End Property Public Overrides Function ToString() As String Return (m_Name & " " & Me.GetType.ToString) End Function End Class
End Class
</source>