ZBasic Language Reference
111
ZBasic Microcontrollers
Function GetSeconds() as Single
GetSeconds = seconds
End Function
Static Function Fmt(ByVal h as Byte, ByVal m as Byte, _
ByVal s as Single) as String
Fmt = CStr(h) & ":" & CStr(m) & ":" & ::Fmt(s, 2)
End Function
Private:
Dim hour as Byte
Dim minute as Byte
Dim seconds as Single
End Class
Sub Main()
Debug.Print MyTime::Fmt(3, 10, 15.7)
End Sub
In this example, a static method named Fmt() is defined that produces a formatted time string. There
are several important aspects of the example to note. Firstly, you may recall that there is a ZBasic
System Library function called Fmt(). This example illustrates how you can create an overload for a
System Library routine. Secondly, within the new Fmt() method, it was convenient to be able to use the
System Library function of the same name. The example code shows how you can use the default
namespace prefix to specify that you want to invoke the System Library routine rather than the Fmt()
method of the MyTime class. Lastly, the invocation of Fmt() in the Main() subroutine shows how the
classname prefix is used to specify that you want to invoke the Fmt() method of the MyTime class.
A static method may invoke only other static methods and procedures defined outside of the class. Also,
it may access only static data members and other data items defined outside of the class. It cannot
access non-static data members because those are specific to an instance.
A static data member can be similarly defined for a class. Access to the data member is achieved using
the classname prefix just as with the static method. It is important to note that there is only one instance
of each static data member in the application no matter how many objects of the class containing the
static data member have been instantiated. The effect is the same as if a module-level variable were
defined outside of the class; the advantage of making it part of the class is that it associates the static
data item with the class to a greater degree than would be the case if a module-level variable were
defined.
It is important to realize that a static method generally must be public for it to be of any value. If it were
private (or protected), it would only be accessible to class (and subclass) methods. For the most part, the
same is true for static data members although there may be special cases where private (or protected)
static data items would be useful.
4.11 Inheritance
The example classes described thus far have derived all of their characteristics and capabilities from the
methods and data members explicitly defined as part of the class. One of the great benefits of object-
oriented programming comes from the ability to define a class that is based on the functionality of an
existing class. Essentially, the idea is to take an existing class and extend it by adding some new
capability without affecting the users of the existing class. Consider the example below.
Class A
Public:
Enum Color
Red
Green
Blue
End Enum
|