请看下面的代码:
Public Class A
Public person1 As Person
End Class
Public Class B
Inherits A
Public Function CheckGender() As Boolean
If person1._Gender = "M" Then
CheckGender = True
End If
End Function
End Class
Public Class C
Inherits B
Public Function CheckAge() As Boolean
If person1._Age > 30 Then
CheckAge = True
End If
End Function
End Class
Public Class D
Inherits C
Public Sub SomeMethod()
Dim list As List(Of Person) = New List(Of Person)
Dim p1 As New Person("Ian", "M", 31)
Dim p2 As New Person("Susan", "F", 20)
Dim p3 As New Person("Mark", "M", 22)
list.Add(p1)
list.Add(p2)
list.Add(p3)
For Each Person As Person In list
person1 = Person
If CheckAge() And CheckGender() Then
'Do something
End If
Next
End Sub
Public Shared Sub Main()
Dim d As New D
d.SomeMethod()
End Sub
End Class
Public Class Person
Public _Name As String
Public _Gender As String
Public _Age As String
Public Sub New(ByVal Name As String, ByVal Gender As String, ByVal Age As Integer)
_Name = Name
_Gender = Gender
_Age = Age
End Sub
End Class
c.SomeMethod
遍历三个人并进行两次检查:b.CheckGender
和c.CheckAge
。 CheckGender
并CheckAge
使用超类中的实例变量A
。
实时环境中的代码每天循环访问数据库中的 100,000 条记录,并删除那些CheckGender
同时CheckAge
为 true 的记录。 在这种情况下使用实例变量是一个糟糕的设计选择吗? 我总是被教导使用局部变量。我希望该对象被Person
传递给每个循环。还是真的不重要?CheckGender
CheckAge
请注意,上面的代码是一个假设的例子。 CheckGender
是实际应用中CheckAge
的复杂功能。