113 of 208
ZBasic Language Reference 105 ZX Microcontroller Family Public Sub _Assign(ByVal src as MyTime) hour = src.hour minute = src.minute seconds = src.seconds End Sub 4.7 Object Self-reference and Parent Reference Occasionally, it is necessary or expedient to be able to refer to the particular object instance in a method. For such purposes, the special identifier _this is recognized. This identifier can be used as a qualifier on a member reference (e.g. _this.hour) or as a prefix to the DataAddress qualifier: _this.DataAddress. Similarly, it is sometimes useful to be able to refer to a method of the parent class of an object. For this purpose, the special identifier _parent can be used as a prefix to a class method, e.g. Call _parent.DoSomething(). This is only necessary, of course, if the class in which the reference occurs has a method with the same signature (name plus parameter types), e.g. it is often useful for invoking a parent’s constructor. If needed, the _parent prefix can be used multiple times to refer to a particular ancestor class, e.g. _parent._parent._parent.DoSomething(). 4.8 Explicit Class References and Default Namespace References In some cases, it may be necessary to refer to a method or data member of a specific class, overriding the method or member that would otherwise be matched by a particular name. You can refer to the namespace of a specific class by adding a class name prefix to the identifier. The class name prefix has the form of the class’ name followed by two colons, e.g. MyTime::hour. The class name prefix is useful inside of class methods as well as in non-class subroutines and functions and for referring to static methods of a class. Occasionally, it may be necessary to refer to an identifier that is outside of a class when the class contains a method or data member having the same name. Adding the default namespace prefix (two colons) to the identifier allows you to refer to the namespace outside of the class hierarchy, e.g. ::foo. 4.9 Class Sections When defining a class, the various members have default visibility attributes identical to those that would apply if the same type of member were being defined outside of a class. For example, if a data member is defined using Dim, it will be private. Similarly the default visibility for a method is public just as it is for subroutines and functions outside of a class. The differing default visibility attributes for different types of member can lead to confusion about the visibility of a particular element of a class. To mitigate this issue, one could adopt the practice of specifying the Public or Private attribute for every member. An alternative strategy is to use section labels in the class to override the default visibility of the members in the section. The MyTime class definition is rewritten below using section labels. Class MyTime Public: Sub _Create(ByVal h as Byte = 0, ByVal m as Byte = 0, _ ByVal s as Single = 0.0) Call SetTime(h, m, s) End Sub Sub SetTime(ByVal h as Byte = 0, ByVal m as Byte = 0, _ ByVal s as Single = 0.0) hour = Min(h, 23) minute = Min(m, 59)