'VB示例:确定各月天数的笨拙做法
If (month = 1) Then
days = 31
ElseIf (month = 2) Then
days = 28
ElseIf (month = 3) Then
days = 31
...
ElseIf (month = 11) Then
days = 30
ElseIf (month = 12) Then
days = 31
End If
' 你需要首先创建出这张表用来存放各个月份的天数
' VB示例:确定各月天数的优雅做法
Dim daysPerMonth() As Integer = _
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
days = daysPerMonth(month-1)
While more messages to read
Read a message header
Decode the message ID from the message header
If the message header is type 1 then
Print a type 1 message
Else if the message header is type 2 then
Print a type 2 message
...
Else if the message header is type 19 then
Print a type 19 message
Else if the message header is type 20 then
Print a type 20 message
如何修改?将动作存储到表中可以将子程序入口地址存储,类似的有多态的实现
1.2.2.5. 画向对象的方法
但是基本结构还是同样复杂
问题的逻辑可以隐藏在对象继承结构里
While more messages to read
Read a message header
Decode the message ID from the message header
If the message header is type 1 then
Instantiate a type 1 message object
Else if the message header is type 2 then
Instantiate a type 2 message object
...
Else if the message header is type 19 then
Instantiate a type 19 message object
Else if the message header is type 20 then
Instantiate a type 20 message object
End if
End While
While more messages to read
Read a message header
Decode the message ID from the message header
Look up the message description in the message-description table
Read the message fields and print them based on the message
description
End While
1.2.7. 消息打印子程序
While more fields to print
Get the field type from the message description
case ( field type )
of ( floating point )
read a floating-point value
print the field label
print the floating-point value
of ( integer )
read an integer value
print the field label
print the integer value
of ( character string )
read a character string
print the field label
print the character string
…
End Case
End While
' set up data for grading table
Dim rangeLimit() As Double = { 50.0, 65.0, 75.0, 90.0, 100.0 }
Dim grade() As String = { "F", "D", "C", "B", "A" }
maxGradeLevel = grade.Length - 1
...
' assign a grade to a student based on the student's score
gradeLevel = 0
studentGrade = "A"
While ( gradeLevel < maxGradeLevel )
If ( studentScore < rangeLimit( gradeLevel ) ) Then
studentGrade = grade( gradeLevel )
End If
gradeLevel = gradeLevel + 1
Wend