JavaScript Editor
JavaScript Debugger|
| ||
The MyClass keyword lets you call an overridable method in your class while making sure that implementation of the method in your class is called instead of an overridden version of the method. Clear as mud, right? Here's an example-say that you have a class Animal with a method named Breathing that displays "Breathing…" in a message box:
Public Class Animal
Overridable Sub Breathing()
MsgBox("Breathing...")
End Sub
End Class
Now suppose you have a method in Animal named, say, Live, that calls Breathing:
Public Class Animal
Sub Live()
Breathing()
End Sub
Overridable Sub Breathing()
MsgBox("Breathing...")
End Sub
End Class
So far, there's no problem. However, what if you derive a class named Fish from Animal, and Fish overrides Breathing with its own version:
Public Class Fish
Inherits Animal
Overrides Sub Breathing()
MsgBox("Bubbling...")
End Sub
End Class
Now, when you create a Fish object and call the Live method, the new overriding version of Breathing will be called, not the original version from the Animal class. To call the original version in the Animal class, you can use MyClass.Breathing in the Animal class, which I do in a new method named Live2:
Public Class Animal
Sub Live()
Breathing()
End Sub
Sub Live2()
MyClass.Breathing()
End Sub
Overridable Sub Breathing()
MsgBox("Breathing...")
End Sub
End Class
This is all part of the MyClass example on the CD-ROM; there are two buttons in that example, "Call Breathing" and "Call MyClass.Breathing"; here's how those buttons work:
Public Class Form1
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code
Dim jaws As New Fish()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
jaws.Live()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
jaws.Live2()
End Sub
End Class
You can see the MyClass example at work in Figure 12.6. When you click the "Call Breathing" button, you'll see "Bubbling…" and when you click "Call MyClass.Breathing", you'll see "Breathing...".
|
| ||
Free JavaScript Editor
JavaScript Editor