VB.Net/Class/Property
Содержание
Class Property Get and Set
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim time As New CTime3() " invoke time6 methods time.Second = (time.Second + 1) time.Minute = (time.Minute + 1) time.Hour = (time.Hour + 1) Console.WriteLine( "Hour: " & time.Hour & "; Minute: " & _ time.Minute & "; Second: " & time.Second ) End Sub
End Class
" Represents time in 24-hour format and contains properties. Class CTime3
Inherits Object " declare Integers for hour, minute and second Private mHour As Integer Private mMinute As Integer Private mSecond As Integer " CTime3 constructor: initialize each instance variable to zero " and ensure that each CTime3 object starts in consistent state Public Sub New() SetTime(0, 0, 0) End Sub " New " CTime3 constructor: " hour supplied, minute and second defaulted to 0 Public Sub New(ByVal hourValue As Integer) SetTime(hourValue, 0, 0) End Sub " New " CTime3 constructor: " hour and minute supplied; second defaulted to 0 Public Sub New(ByVal hourValue As Integer, _ ByVal minuteValue As Integer) SetTime(hourValue, minuteValue, 0) End Sub " New " CTime3 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 " CTime3 constructor: another CTime3 object supplied Public Sub New(ByVal timeValue As CTime3) SetTime(timeValue.mHour, timeValue.mMinute, _ timeValue.mSecond) End Sub " New " set new time value using universal time; " uses properties to perform validity checks on data Public Sub SetTime(ByVal hourValue As Integer, _ ByVal minuteValue As Integer, ByVal secondValue As Integer) Hour = hourValue " looks Minute = minuteValue " dangerous Second = secondValue " but it is correct End Sub " SetTime " property Hour Public Property Hour() As Integer " return mHour value Get Return mHour End Get " set mHour value Set(ByVal value As Integer) If (value >= 0 AndAlso value < 24) Then mHour = value Else mHour = 0 End If End Set End Property " Hour " property Minute Public Property Minute() As Integer " return mMinute value Get Return mMinute End Get " set mMinute value Set(ByVal value As Integer) If (value >= 0 AndAlso value < 60) Then mMinute = value Else mMinute = 0 End If End Set End Property " Minute " property Second Public Property Second() As Integer " return mSecond value Get Return mSecond End Get " set mSecond value Set(ByVal value As Integer) If (value >= 0 AndAlso value < 60) Then mSecond = value Else mSecond = 0 End If End Set End Property " Second
End Class
</source>
Compare int property
<source lang="vbnet"> Imports System.Collections.Generic Public Class Car
Public PetName As String = String.Empty Public Color As String = String.Empty Public Speed As Integer Public Make As String = String.Empty Public Overloads Overrides Function ToString() As String Return String.Format("Make={0}, Color={1}, Speed={2}, PetName={3}", _ Make, Color, Speed, PetName) End Function
End Class Module Program
Sub Main() Dim myCars As New List(Of Car)(New Car() { _ New Car With {.PetName = "D", .Color = "Silver", .Speed = 100, .Make = "BMW"}, _ New Car With {.PetName = "C", .Color = "Tan", .Speed = 90, .Make = "BMW"}, _ New Car With {.PetName = "B", .Color = "Rust", .Speed = 5, .Make = "Yugo"}, _ New Car With {.PetName = "A", .Color = "White", .Speed = 43, .Make = "Ford"}}) Dim fastCars = From c In myCars Where c.Speed > 55 Select c For Each car In fastCars Console.WriteLine(car.PetName) Next End Sub
End Module
</source>
Declare Protected Properties
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() Dim e As New Programmer("Joe", 15) e.RaiseSalary(0.1D) "Console.WriteLine(e.TheName & " salary is now " & e.Salary()) " Compile Error End Sub
End Class
Public Class Employee
Private m_Name As String Private m_Salary As Decimal Public Sub New(ByVal theName As String, ByVal curSalary As Decimal) m_Name = theName m_Salary = curSalary End Sub Public ReadOnly Property TheName() As String Get Return m_Name End Get End Property Protected ReadOnly Property Salary() As Decimal Get Return MyClass.m_Salary End Get End Property Public Overridable Overloads Sub RaiseSalary(ByVal Percent As Decimal) m_Salary = (1 + Percent) * m_Salary End Sub
End Class Public Class Programmer
Inherits Employee Public Sub New(ByVal theName As String, ByVal curSalary As Decimal) MyBase.New(theName, curSalary) End Sub Public Overloads Overrides Sub RaiseSalary(ByVal Percent As Decimal) MyBase.RaiseSalary(1.2D * Percent) End Sub
End Class
</source>
Define and use Class: Property
<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() Dim objCar As New Car "Set the Color property to Red objCar.Color = "Red" "Show what the value of the property is Console.WriteLine("My car is this color:") Console.WriteLine(objCar.Color)
"Report the speed Console.WriteLine("The car"s speed is:") Console.WriteLine(objCar.Speed) "Accelerate objCar.Accelerate(5) "Report the new speed Console.WriteLine("The car"s speed is now:") Console.WriteLine(objCar.Speed)
"Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors) "Try changing the number of doors to 1000 objCar.NumberOfDoors = 1000 "Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors) "Now try changing the number of doors to 2 objCar.NumberOfDoors = 2 "Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors)
"Accelerate the car to 25mph objCar.Accelerate(25) "Report whether or not the car is moving If objCar.IsMoving = True Then Console.WriteLine("The car is moving.") Else Console.WriteLine("The car is stopped.") End If End Sub
End Class
Public Class Car Implements IDisposable Public Color As String Public HorsePower As Integer Private _speed As Integer Private _numberOfDoors As Integer Public ReadOnly Property Speed() As Integer Get Return _speed End Get End Property Public Sub Accelerate(ByVal accelerateBy As Integer) "Adjust the speed _speed += accelerateBy End Sub Public Property NumberOfDoors() As Integer Get Return _numberOfDoors End Get Set(ByVal value As Integer) "Is the new value between two and five If value >= 2 And value <= 5 Then _numberOfDoors = value End If End Set End Property Public Function IsMoving() As Boolean "Is the car"s speed zero? If Speed = 0 Then Return False Else Return True End If End Function "Constructor Public Sub New() "Set the default values Color = "White" _speed = 0 _numberOfDoors = 5 End Sub Public Overridable Function CalculateAccelerationRate() As Double Return 4.2 End Function Private disposed As Boolean = False " IDisposable Private Overloads Sub Dispose(ByVal disposing As Boolean) If Not Me.disposed Then If disposing Then " TODO: put code to dispose managed resources End If " TODO: put code to free unmanaged resources here End If Me.disposed = True End Sub
- Region " IDisposable Support "
" This code added by Visual Basic to correctly implement the disposable pattern. Public Overloads Sub Dispose() Implements IDisposable.Dispose " Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub Protected Overrides Sub Finalize() " Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(False) MyBase.Finalize() End Sub
- End Region
End Class
</source>
Get and set Properties
<source lang="vbnet"> Imports System.IO Module Module1
Sub Main() Dim Palm As New Book("Book 1", "Author 1") Dim Upgrading As New Book("Book 2", "Author 2") Palm.Price = 49.99 Palm.Pages = 2000 Upgrading.Price = 119.99 Upgrading.Pages = 600 Console.WriteLine(Upgrading.Pages) End Sub
End Module
Class Book Public Title As String Public Author As String Private BookPrice As Double Private BookPages As Integer Public Property Price() As Double Get Return BookPrice End Get Set(ByVal Value As Double) If (Value >= 0) And (Value <= 100) Then BookPrice = Value Else Console.WriteLine("Invalid price for " & Title) BookPrice = 0 End If End Set End Property Public Property Pages() As Integer Get Return BookPages End Get Set(ByVal Value As Integer) If (Value >= 0) And (Value <= 1500) Then BookPages = Value Else Console.WriteLine("Invalid page count for " & Title) BookPages = 0 End If End Set End Property Public Sub New(ByVal Title As String, ByVal Author As String) Me.Title = Title Me.Author = Author End Sub End Class </source>
Property Shadow during Inheritance
<source lang="vbnet"> Imports System Imports System.Diagnostics Public Class MainClass
Shared Sub Main() Dim mgr As New Manager mgr.LastName = "Manager Last Name" mgr.EmployeeId = 1 Dim emp As Employee = CType(mgr, Employee) emp.LastName = "Employee Last Name" emp.EmployeeId = 2 Dim per As Person = CType(mgr, Person) per.LastName = "Person Last Name" per.EmployeeId = "A" Console.WriteLine(mgr.EmployeeId & ": " & mgr.LastName) Console.WriteLine(emp.EmployeeId & ": " & emp.LastName) Console.WriteLine(per.EmployeeId & ": " & per.LastName) End Sub
End Class Public Class Person
Public LastName As String Public EmployeeId As String
End Class Public Class Employee
Inherits Person Public Shadows EmployeeId As Long
End Class Public Class Manager
Inherits Employee Public Shadows LastName As String
End Class
</source>
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim e As New Employee("Joe", 100000) Console.WriteLine(e.TheName & " is employee# " & e.EmployeeId & " with salary " & e.Salary()) Dim Sally As New Employee("Sally", 150000) Console.WriteLine(Sally.TheName & " is employee# " & _ Sally.EmployeeId & " with salary " & Sally.Salary()) Console.WriteLine("Please press the Enter key") End Sub
End Class
Public Class Employee
Private m_Name As String Private m_Salary As Decimal Private Shared m_EmployeeID As Integer = 10000 Private m_MyID As Integer Public Sub New(ByVal thesName As String, ByVal curSalary As Decimal) m_Name = thesName m_Salary = curSalary m_EmployeeID = m_EmployeeID + 1 m_MyID = m_EmployeeID End Sub ReadOnly Property EmployeeId() As Integer Get Return m_MyID End Get End Property ReadOnly Property TheName() As String Get TheName = m_Name End Get End Property ReadOnly Property Salary() As Decimal Get Salary = m_Salary End Get End Property
End Class
</source>