Vb.net 如何保留';新';跨子类的构造函数?
让我们从一个示例模块开始:Vb.net 如何保留';新';跨子类的构造函数?,vb.net,Vb.net,让我们从一个示例模块开始: Module PuppyKillers Public Puppies As Double = 135 Public SquadSize As Integer = 5 Class PuppyKiller Private KillingTimer As New System.Timers.Timer _ With {.AutoReset = True, .Interval = 1000, .Enabled =
Module PuppyKillers
Public Puppies As Double = 135
Public SquadSize As Integer = 5
Class PuppyKiller
Private KillingTimer As New System.Timers.Timer _
With {.AutoReset = True, .Interval = 1000, .Enabled = False}
Public PuppiesPerSecond As Double = 0.5
Public name As String = "John Doe"
Public Sub New(Optional param As Double = 1)
PuppiesPerSecond = PuppiesPerSecond * param
AddHandler KillingTimer.Elapsed, AddressOf KillPuppies
End Sub
Private Sub KillPuppies(ByVal sender As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
If Puppies <= 0 Then
Me.Killing = False
Else
Puppies -= PuppiesPerSecond
End If
End Sub
Property Killing As Boolean
Get
Return KillingTimer.Enabled
End Get
Set(value As Boolean)
KillingTimer.Enabled = value
End Set
End Property
End Class
Class ChiefPuppyKiller
Inherits PuppyKiller
End Class
Sub Exterminators_Start() ' 4 Killers + 1 Chief
Dim squad As New ArrayList
REM The following line prevents the compilation.
squad.Add(New ChiefPuppyKiller(3)) 'A chief kills 3 times the normal amount.
For i As Integer = 1 To SquadSize - 1
squad.Add(New PuppyKiller)
Next
REM Start the slaughter
Console.WriteLine("Puppies: " & Puppies)
For Each c As PuppyKiller In squad
c.Killing = True
Next
Do
Threading.Thread.Sleep(4737)
Console.WriteLine("Remaining Puppies: " & Math.Ceiling(Puppies))
Application.DoEvents()
If Puppies = 0 Then
Console.WriteLine("Meow: No more puppies.")
Exit Do
End If
Loop
End Sub
End Module
模块木偶填充器
公犬双倍=135
公共分组大小为整数=5
木偶类
私人KillingTimer作为新的System.Timers.Timer_
使用{.AutoReset=True、.Interval=1000、.Enabled=False}
公共小狗每秒双精度=0.5
公共名称为String=“John Doe”
Public Sub New(可选参数为Double=1)
PuppiesPerSecond=PuppiesPerSecond*参数
AddHandler KillingTimer.已用,KillingPuppies的地址
端接头
私家子猎犬(ByVal发送者作为对象_
ByVal e As System.Timers.ElapsedEventArgs)
如果Puppies,则必须为要传入该值的每个类添加一个新构造函数:
Class ChiefPuppyKiller
Inherits PuppyKiller
Public Sub New(param As Double)
MyBase.New(param)
End Sub
End Class