VB.Net/Class/Class Define

Материал из VB Эксперт
Версия от 12:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Class Composition

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim s As New CStudent( _
         "A", "B", 7, 24, 1949, 3, 12, 1988)
      Console.WriteLine(s.ToStandardString() )
      
    End Sub
End Class
" Encapsulates month, day and year.
Class CDay
   Inherits Object
   Private mMonth As Integer " 1-12
   Private mDay As Integer " 1-31 based on month
   Private mYear As Integer " any year
   Public Sub New(ByVal monthValue As Integer, _
      ByVal dayValue As Integer, ByVal yearValue As Integer)
      mMonth = monthValue
      mYear = yearValue
      mDay = dayValue
   End Sub " New
   " create string containing month/day/year format
   Public Function ToStandardString() As String
      Return mMonth & "/" & mDay & "/" & mYear
   End Function
End Class 

" Represent student name, birthday and hire date.
Class CStudent
   Inherits Object
   Private mFirstName As String
   Private mLastName As String
   Private mBirthDate As CDay " member object reference
   Private mHireDate As CDay " member object reference
   " CStudent constructor
   Public Sub New(ByVal firstNameValue As String, _
      ByVal lastNameValue As String, _
      ByVal birthMonthValue As Integer, _
      ByVal birthDayValue As Integer, _
      ByVal birthYearValue As Integer, _
      ByVal hireMonthValue As Integer, _
      ByVal hireDayValue As Integer, _
      ByVal hireYearValue As Integer)
      mFirstName = firstNameValue
      mLastName = lastNameValue
      " create CDay instance for employee birthday
      mBirthDate = New CDay(birthMonthValue, birthDayValue, _
         birthYearValue)
      " create CDay instance for employee hire date
      mHireDate = New CDay(hireMonthValue, hireDayValue, _
         hireYearValue)
   End Sub " New
   " return employee information as standard-format String
   Public Function ToStandardString() As String
      Return mLastName & ", " & mFirstName & " Hired: " _
         & mHireDate.ToStandardString() & " Birthday: " & _
         mBirthDate.ToStandardString()
   End Function " ToStandardString
End Class " CStudent


Define and use your own Time Class

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim time As New CTime() " call CTime constructor
      Console.WriteLine( "The initial universal times is: " & _
         time.ToUniversalString() & vbCrLf & _
         "The initial standard time is: " & _
         time.ToStandardString() )
      time.SetTime(13, 27, 6) " set time with valid settings
      Console.WriteLine( "Universal time after setTime is: " & _
         time.ToUniversalString() & vbCrLf & _
         "Standard time after setTime is: " & _
         time.ToStandardString() )
      time.SetTime(99, 99, 99) " set time with invalid settings
      Console.WriteLine( "After attempting invalid settings: " & vbCrLf & _
         "Universal time: " & time.ToUniversalString() & _
         vbCrLf & "Standard time: " & time.ToStandardString() )

    End Sub
End Class

Class CTime
   Inherits Object
   Private mHour As Integer " 0 - 23
   Private mMinute As Integer " 0 - 59
   Private mSecond As Integer " 0 - 59
   Public Sub New()
      SetTime(0, 0, 0)
   End Sub " New
   Public Sub SetTime(ByVal hourValue As Integer, _
      ByVal minuteValue As Integer, ByVal secondValue As Integer)
      If (hourValue >= 0 AndAlso hourValue < 24) Then
         mHour = hourValue
      Else
         mHour = 0
      End If
      If (minuteValue >= 0 AndAlso minuteValue < 60) Then
         mMinute = minuteValue
      Else
         mMinute = 0
      End If
      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
   " convert to String in standard-time format
   Public Function ToStandardString() As String
      Dim suffix As String = " PM"
      Dim format As String = "{0}:{1:D2}:{2:D2}"
      Dim standardHour As Integer
      " determine whether time is AM or PM
      If mHour < 12 Then
         suffix = " AM"
      End If
      " convert from universal-time format to standard-time format
      If (mHour = 12 OrElse mHour = 0) Then
         standardHour = 12
      Else
         standardHour = mHour Mod 12
      End If
      Return String.Format(format, standardHour, mMinute, _
         mSecond) & suffix
   End Function " ToStandardString
End Class " CTime


Nested Class Demo

Imports System
Public Class MainClass
    
    Shared Sub Main()
         Dim f1 As New Fraction(3, 4)
         Console.WriteLine("f1: {0}", f1.ToString(  ))
         Dim fa As New Fraction.FractionArtist(  )
         fa.Draw(f1)
    End Sub
End Class

 Public Class Fraction
     Private numerator As Integer
     Private denominator As Integer
     Public Sub New( _
       ByVal numerator As Integer, ByVal denominator As Integer)
         Me.numerator = numerator
         Me.denominator = denominator
     End Sub "New
     Public Overrides Function ToString(  ) As String
         Return [String].Format("{0}/{1}", numerator, denominator)
     End Function "ToString
     " Nested Class
     Class FractionArtist
         Public Sub Draw(ByVal f As Fraction)
             Console.WriteLine("Drawing the numerator: {0}", f.numerator)
             Console.WriteLine( _
                 "Drawing the denominator: {0}", f.denominator)
         End Sub "Draw
     End Class "FractionArtist
 End Class "Fraction


Your Complex Number Class

There is a minor bug though. The unary minus operator is incorrect.
    Public Shared Operator -(ByVal c1 As Complex) As Complex
        Return New Complex(c1.imaginaryPart, c1.realPart)
    End Operator
This should be replaced with
    Public Shared Operator -(ByVal c1 As Complex) As Complex
        Return New Complex(-c1.realPart, -c1.imaginaryPart)
    End Operator
and perhaps an additional function like
    Public function Conjungate() As Complex
        Return New Complex(realPart, -imaginaryPart)
    End Function
And you might consider adding a .Length function equal to the current narrowing.
(And mathematically preferable replacing it, since converting complex->real isn"t well-defined)
Best regards
Eske Rahn
rahn at sol.dk       
       
       
Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
        Dim X, Y As Complex
        X = New Complex(1, 2)
        Y = New Complex(3, 4)
        Console.WriteLine( (X + Y).ToString )
        Console.WriteLine( (X - Y).ToString )
        Console.WriteLine( (X * Y).ToString )
        Console.WriteLine( (X = Y).ToString )
        Console.WriteLine( (X <> Y).ToString )
        Console.WriteLine( (-X).ToString )
        Dim abs_x As Double = CDbl(X)
        Console.WriteLine(  abs_x.ToString )
    End Sub
End Class
Public Class Complex
    Public realPart As Double
    Public imaginaryPart As Double
    " Constructors.
    Public Sub New()
    End Sub
    Public Sub New(ByVal real_part As Double, ByVal imaginary_part As Double)
        realPart = real_part
        imaginaryPart = imaginary_part
    End Sub
    " ToString.
    Public Overrides Function ToString() As String
        Return realPart.ToString & " + " & imaginaryPart.ToString & "i"
    End Function
    " Operators.
    Public Shared Operator *(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex( _
            c1.realPart * c2.realPart - c1.imaginaryPart * c2.imaginaryPart, _
            c1.realPart * c2.imaginaryPart + c1.imaginaryPart * c2.realPart)
    End Operator
    Public Shared Operator +(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex( _
            c1.realPart + c2.realPart, _
            c1.imaginaryPart + c2.imaginaryPart)
    End Operator
    Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex( _
            c1.realPart - c2.realPart, _
            c1.imaginaryPart - c2.imaginaryPart)
    End Operator
    Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
        Return (c1.realPart = c2.realPart) AndAlso (c1.imaginaryPart = c2.imaginaryPart)
    End Operator
    Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
        Return (c1.realPart <> c2.realPart) OrElse (c1.imaginaryPart <> c2.imaginaryPart)
    End Operator
    Public Shared Operator -(ByVal c1 As Complex) As Complex
        Return New Complex(c1.imaginaryPart, c1.realPart)
    End Operator
    Public Shared Narrowing Operator CType(ByVal c1 As Complex) As Double
        Return System.Math.Sqrt(c1.realPart * c1.realPart + c1.imaginaryPart * c1.imaginaryPart)
    End Operator
End Class