Các bạn coi kĩ( hoặc copy về Test) và giải thích dùm mình nha.
Class Day
Code:
' Day.vb
' Encapsulates month, day and year.
Class Day
Private monthValue As Integer ' 1-12
Private dayValue As Integer ' 1-31 based on month
Private yearValue As Integer ' any year (could validate)
' constructor confirms proper value for month, then calls
' method CheckDay to confirm proper value for day
Public Sub New(ByVal mm As Integer, _
ByVal dd As Integer, ByVal yy As Integer)
Month = mm
Day = dd
Year = yy
End Sub ' New
' property Month
Public Property Month() As Integer
Get
Return monthValue
End Get
Set(ByVal mm As Integer)
' ensure month value is valid (in the range 1-12)
If (mm > 0 AndAlso mm <= 12) Then
monthValue = mm
Else
monthValue = 1 ' to ensure consistent state
' inform user of error
Console.WriteLine("Invalid month (" & mm & _
") set to 1.")
End If
End Set
End Property ' Month
' property Day
Public Property Day() As Integer
Get
Return dayValue
End Get
Set(ByVal dd As Integer)
dayValue = CheckDay(dd) ' validate dayPassed
End Set
End Property ' Day
' property Year
Public Property Year() As Integer
Get
Return yearValue
End Get
Set(ByVal yy As Integer)
yearValue = yy
End Set
End Property ' Year
' confirm proper day value based on month and year
Private Function CheckDay(ByVal testDayValue As Integer) As Integer
Dim daysPerMonth() As Integer = _
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
' validate day
If (testDayValue > 0 AndAlso _
testDayValue <= daysPerMonth(Month)) Then
Return testDayValue
End If
' check for leap year in February
If (Month = 2 AndAlso testDayValue = 29 AndAlso _
((Year Mod 400 = 0) OrElse (Year Mod 4 = 0 AndAlso Year Mod 100 <> 0))) Then
Return testDayValue
End If
' inform user of error
Console.WriteLine("Invalid day (" & testDayValue & ") set to 1. ")
Return 1 ' leave object in consistent state
End Function ' CheckDay
' create string containing month/day/year format
Public Overrides Function ToString() As String
Return (Month & "/" & Day & "/" & Year)
End Function ' ToString
End Class ' Day
Class Employee
Code:
' Employee.vb
' Represent employee name, birthday and hire date.
Class Employee
Private firstName As String
Private lastName As String
Private birthDate As Day ' member object reference
Private hireDate As Day ' member object reference
' Employee constructor
Public Sub New(ByVal first As String, ByVal last As String, _
ByVal birthMonth As Integer, ByVal birthDay As Integer, _
ByVal birthYear As Integer, ByVal hireMonth As Integer, _
ByVal hireDay As Integer, ByVal hireYear As Integer)
firstName = first
lastName = last
' create Day instance for employee birthday
birthDate = New Day(birthMonth, birthDay, birthYear)
' create Day instance for employee hire date
hireDate = New Day(hireMonth, hireDay, hireYear)
End Sub ' New
' return employee information as String
Public Overrides Function ToString() As String
Return (lastName & ", " & firstName & " Hired:" _
& hireDate.ToString() & " Birthday:" & birthDate.ToString())
End Function ' ToString
End Class ' Employee
Module CompositionTest
Code:
' CompositionTest.vb
' Demonstrate an object with member object reference.
Module CompositionTest
Sub Main()
Dim employee As New Employee( _
"Bob", "Blue", 2, 29, 2001, 2, 29, 2008)
Console.WriteLine(employee.ToString())
Console.ReadLine()
End Sub ' Main
End Module ' CompositionTest
Đây là bài demo trong một cuốn sách mình đang học.(Visual Basic® 2005 for Programmers: Deitel® Developer Series, Second Edition)
Mình đã Test rất nhiều lần nhưng vẫn ko hiểu tại sao,rõ ràng mình truyền giá trị một năm ko phải nhuận(2001) thế nhưng nó vẫn xuất ra tháng 2 có 29 ngày (đáng ra phải xuất 1 dòng báo lỗi và thay cái ngày lỗi đó bằng ngày 1)
thế nhưng như ở dưới đây các bạn thấy đó tháng 2 năm 2001 vẫn có 29 ngày.
Code:
Blue, Bob Hired: 2/29/2008 Birthday: 2/29/2001
Mình copy nguyên xi trong cuốn sách nên loại trừ trường hợp gõ sai,ko lẽ do tác giả nhưng mình kiểm tra rất kĩ cách tính năm nhuận rồi và cũng ko có sai sót gì hết.
Mong cao thủ giải cái khúc mắc này dùm,cám ơn nhiều.
Bookmarks