120 of 208
ZBasic Language Reference 112 ZX Microcontroller Family Including one or more other classes as mixins of a given class is accomplished using the Includes keyword as shown in the example below. If more than one mixin class is specified, the mixin class names are comma-separated. Class A Public: Sub DoSomething() End Sub Protected: Private: End Class Class B Includes A Public: Sub Identify() Call DoSomething() End Sub End Class Dim MyB as B Sub Main() Call MyB.DoSomething() End Sub One restriction on using mixins is that the public and protected methods and data members of each mixin must be unique among all of the mixins included and must not duplicate any method or data member names of the including class. This restriction is necessary because all public and protected methods and data members need to reside in the namespace of the including class and therefore must be unique. It is possible, however, to have methods of the same name in multiple mixins and/or the including class as long as they each have unique signatures, i.e. they must have different number and/or types of parameters. Note, however, that the including class may contain methods that overload methods of mixin classes. It is important to be aware that a mixin class method that is marked as Abstract must be overloaded by the including class. Finally, if a class is defined using both inheritance and mixins, the Extends <class name> specification must appear before the Includes <class name list> specification, e.g. Class D Extends B Includes A, C Public: Sub DoSomething() End Sub End Class 4.15 Using the Const Attribute for Methods Earlier in this chapter, it was stated that an object may be passed to a subroutine or function ByRef or ByVal. If it is passed ByVal, the object instance is considered to be “read-only” within the receiving procedure. As such, the procedure has read access to the public data members of the object but it is not allowed to modify those data members. Also, the receiving procedure may not pass the object ByRef to any other procedure or method. Additionally, in order to invoke a method of the object, that method must be declared to be “constant”, meaning that it isn’t allowed to modify the object or pass the object by reference to another method or procedure. To define a class method as constant, add the keyword Const following the closing parenthesis of the parameter list. Optionally, for methods that are functions, you may instead place the Const keyword after the function’s return type. An example is shown below using a previously defined method. Sub Identify() Const