Qore Programming Language Reference Manual
1.12.2
|
Classes define types of Qore objects. Classes can define members and methods, which are attributes of the class and functions that operate only on the objects of that class, respectively. Furthermore access to class members and methods can be restricted with the private
or private:internal
access modifiers, and classes can be subclassed to support polymorphism.
A class can have any valid identifier name except "auto"
but may not have the same name as a child namespace of the namespace parent object.
[public] [final] class [
namespace_path::...]
class_identifier] [inherits [private[:internal|hierarchy]|public] [
namespace_path::...]
parent_class_identifier[, ...]] {
[[private[:internal|hierarchy]] [transient]
member_name[, ...];]
[private[:internal|hierarchy]|public {
[[transient] [
member_type]
member_name [=
initialization_expression];]
[static [
static_member_type]
static_member_name [=
initialization_expression];]
[const
constant_name =
initialization_expression;]
}]
[[private[:internal|hierarchy]|public] [deprecated] constructor([
[param_type] param_name [=
default_initialization_expression], ...]) [:
parent_class_name(
args...), ...] {
}]
[copy(object
arg_name) {
}]
[destructor() {
}]
[[static|final] [synchronized] [private[:internal|hierarchy]|public] [deprecated] [
return_type]
method_name([
[param_type] param_name [=
default_initialization_expression], ...]) {
}]
[[abstract] [private[:internal|hierarchy]|public] [
return_type]
method_name([
[param_type] param_name [=
default_initialization_expression], ...]);]
...
}
When defining a user module, class declarations can be preceded by public, which means that the class will be available (imported) in the Program object importing the module. When a class is declared public outside of a user module, it means that the class can be inherited in any child Program objects created in the same scope. See public for more information. Note that classes can also be imported singly by using the Program::importClass() method.
";"
) is required to terminate a class declaration (in fact using a semicolon would raise a parse exception).[public] class [
namespace_path::...]
class_identifier [inherits [private[:internal|hierarchy]|public] [
namespace_path::...]
parent_class_identifier[, ...]];
[[private[:internal|hierarchy]|public] [deprecated] [
namespace_path::...]
class_identifier::constructor([
[param_type] param_name [=
default_initialization_expression], ...]) [:
parent_class_name(
args...), ...] {
}]
[[static|final] [synchronized] [private[:internal|hierarchy]|public] [deprecated] [
return_type] [
namespace_path::...]
class_identifier::
method_name([
[param_type] param_name [=
default_initialization_expression], ...]) {
}]
[abstract [private[:internal|hierarchy]|public] [
return_type] [
namespace_path::...]
class_identifier::
method_name([
[param_type] param_name [=
default_initialization_expression], ...]);
"$"
or "$."
prefixes can be used with variable or method or member names. Class members, class constants, and static class variables can only be declared in an in-line class definition (the first example above). If a class has at least one public member declared (or inherits a class with at least one public member declared), then only those members declared as public can be accessed from outside the class, and from within the class only members explicitly declared can be accessed as well (unless the class also defines a memberGate() method). In this way typographical errors in member names can be caught (at parse time if types are declared)."-x"
or "--exec-class"
command-line arguments to tell Qore to instantiate the class instead of doing normal top-level execution (in fact, the "--exec-class"
argument disallows the use of top-level statements entirely). For more information, please see qore Executable Command-Line Processing and Parse Directives.final
cannot be subclassed, and, by extension, no methods of a final
class can be reimplemented as well.final
classes.public
access modifier as well. Public methods have no class protection and can be called from any code context.private
access modifier can only be called by other member methods of the same class or in the same class hierarchy. Any attempt to call these methods from outside the class hierarchy will result in a run-time exception. This access modifier, when used without any modifiers, is equivalent to private:hierarchy
and is functionally identical to C++'s protected access modifier.private:internal
are not available outside the class, even in classes in the same class hierarchy, providing strong encapsulation of the declared method. This can be used to ensure that the declared method can be changed in later versions of the class without affecting API compatibility.private:hierarchy
are equivalent to those declared private
and are only private from outside the class hierarchy.synchronized
keyword will only run in one thread at a time.synchronized
normal class methods share a reentrant lock associated with the object, while synchronized
static class methods share a reentrant lock associated with the class itself.static
keyword are like regular functions that are attached to the class. These methods are not associated with a particular object's state and therefore are not allowed to refer to object members or call non-static methods. Also, no reference to the special self
variable is allowed within static methods.private
, private:internal
, or public
or synchronized
like non-static methods; static methods can also access private members of a class (through an object of the class for non-static members). Static methods may not be also abstract.abstract
keyword define methods interfaces that must be defined in child classes for the class to be instantiated. Methods defining the declared interfaces in child classes must define exactly the same parameters in order for the abstract method to match, but the return type of the concrete method in the child class has to simply be compatible with the return type of the abstract method in the parent class.";"
). Furthermore, abstract methods cannot be also static.abstract
keyword can only be used in abstract method declaration; the use of this keyword with a method declaration with a method body will cause a parse exception to be thrown.Name | Description |
constructor( [params...]) {} | Called when objects are created when instantiated by a variable declaration with a class type and constructor arguments or explicitly with the new operator. User code may not explicitly call constructor() methods directly. In a class tree, constructor() methods are called for base classes first in left-to-right, depth-first declaration order.constructor() methods may be overloaded and also private constructors may be defined. Constructors declared private can only be called from within the class hierarchy, and those declared private:internal can only be called within the class itself. |
copy() {} | When a user explicitly calls a copy method, Qore will generate a new object with references to the same members as the source object. Then, if there are any base classes, base class copy() methods are called in the same order as the constructor() methods. If a copy() method is defined, it will be run in the new object with a reference to the old object passed as the first argument. Any other arguments passed to the copy() method are ignored.copy() methods cannot be overloaded and cannot be private. |
destructor() {} | Called when objects are implicitly collected (see Garbage Collection and Resource Management) or are explicitly deleted. User code may not explicitly call destructor() methods. In a class tree, destructor() methods are called for base classes in the opposite order in which the constructors are called.destructor() methods cannot be overloaded and cannot be private. |
any memberGate( string $ member_param_name) {} | If this method is implemented in the class, it is called when read access is attempted to private member or members that do not exist in the current object; the return value of this method is returned as the value of the member.memberGate() methods cannot be overloaded and are not inherited by subclasses. |
any methodGate( string $ method_param_name, ...) {} | If this method is implemented in the class, it is called when methods are called on the object that do not exist in the current object and the return value of this method is returned as the value of the method call.methodGate() methods cannot be overloaded and are not inherited by subclasses. |
memberNotification( string $ member_param_name) {} | If this method is implemented in the class, it is called when an object member is updated outside the class with the member name as the argument. Note that this method is called after the member has been updated and without locking; the call is not atomic respective to other threads that also may update the same member simultaneously.memberNotification() methods cannot be overloaded and are not inherited by subclasses. |
final
cannot be reimplemented in a subclass. Methods can be declared final
to prevent a subclass from altering the behavior of a method that is critical to the class's functionality.final
:final
final
final
Currently the Qore standard class library has no final
methods.
private
, private:internal
, or public
. The following are examples of class constant definitions:private
, private:internal
, or public
. The following are examples of static class variable definitions:If a class has at least one public member declared (or inherits a class with at least one public member declared), then only those members declared as public can be accessed from outside the class, and from within the class only members explicitly declared can be accessed as well (unless the class also defines a memberGate() method). In this way typographical errors in member names can be caught (at parse time if types are declared).
Members declared private
can only be accessed within the class hierarchy; trying to access private members form outside the class hierarchy will result in either a parse or runtime exception, depending on when the illegal access is caught. This access modifier, when used without any modifiers, is equivalent to private:hierarchy
and is functionally identical to C++'s protected access modifier.
Members declared as private:internal
are not available outside the class, even in classes in the same class hierarchy, providing strong encapsulation of the declared member. This can be used to ensure that the declared member can be changed in later versions of the class without affecting API compatibility.
Members declared as private:hierarchy
are equivalent to those declared private
and are only private from outside the class hierarchy.
Members declared with the transient
keyword will not be processed with object serialization and will get their default value (if any) when deserialized.
When defining a class when using the old style syntax, members of instantiated objects are referred to with a special syntax as follows:
$.
member_name
Furthermore, the automatic variable self
is instantiated in every non-static method, representing the current object (similar to this
in C++ or Java). Therefore if you need to access hash members which are not valid Qore identifiers, then enclose the member name in double quotes after the dot operator as follows:
If the class implements a memberGate()
method, then whenever a non-existent member of the class is accessed (read), this method will be called with the name of the member as the sole argument, so that the class can create the member (or react in some other way) on demand. This method is also called when methods of the same class try to access (read) non-existent methods, but is not called from within the memberGate()
method itself.
memberGate()
methods are not inherited; they must be explicitly implemented in each classTo monitor writes to the object, the class can implement a memberNotification()
method, which is called whenever an object member is modified from outside class member code. In this case, the memberNotification()
method is called with the name of the member that was updated so that an object can automatically react to changes to its members (writes to members) from outside the class. This method is not called when members are updated from within class member code.
memberNotification()
methods are not inherited; they must be explicitly implemented in each classMembers that have intialization expressions in the class definition are initialized before the constructor is executed (but after any base class constructors have run). An exception raised in a member initialization expression will cause the constructor to fail and for the object to be deleted immediately.
argv
local variable is instantiated as usual in all class methods where there are more arguments than variables declared in the method declaration.[[
namespace_path::...]
parent_class_name::]$.
method_name([
args, ...])
"$."
prefix as in the following example:METHOD-IS-PRIVATE
(if the method is private) or BASE-CLASS-IS-PRIVATE
(if the method resolves to a privately-inherited base class) exception is raised.methodGate()
method, then whenever a non-existent method of the class is called, the methodGate() method will be called with the name of the member as the first argument (prepended to the other arguments to the non-existant method), so that the class simulate or redirect the method call. This method is also called when methods of the same class try to call non-existent methods, but is not called from within the methodGate()
method itself.methodGate()
methods are not inherited; they must be explicitly implemented in each classClass inheritance is a powerful concept for easily extending and reusing object-oriented code, but is also subject to some limitations. This section explains how class inheritance works in Qore.
Classes inherit the methods of a parent class by using the inherits
keyword as specified above. Multiple inheritance is supported; a single Qore class can inherit one or more classes. When a class is inherited by another class, it is called a base class or parent class. Private inheritance is specified by including the private
access modifier before the inherited class's name. When a class is privately inherited, it means that the inherited class's public declarations (members, constants, methods, etc) are treated as private in the context of accesses outside the class hierarchy.
To limit access of a base class to only the directly inheriting class, use the private:internal
access modifier. Classes inherited with private:internal
are not accessible to the hierarchy in general but rather only to the directly inheriting class.
Inheritance is public
by default, to inherit a class privately, such that the inherted class is accessible within all child classes in the hierarchy, use the private
access modifier before the class name or class path to inherit as follows:
To inherit a class where the inherited class is only available to the inheriting class but not to child classes, use the private:internal
access modifier before the class name or class path as follows:
It is not legal to directly inherit the same class directly more than once; that is; it is not legal to list the same class more than once after the inherits
keyword. However, it is possible that a base class could appear more than once in the inheritance tree if that class is inherited separately by two or more classes in the tree.
In this case, the base class will actually only be inherited once in the subclass, even though it appears in the inheritance tree more than once. This must be taken into consideration when designing class hierarchies, particularly if base class constructor parameters for that class are explicitly provided in a different way by the inheriting classes.
Subclasses can give explicit arguments to their base class constructors using a special syntax (only available to subclass constructors) similar to the C++ syntax for the same purpose as follows:
[[private[:internal|hierarchy]|public] [deprecated] constructor([
[param_type] $
param_name [=
default_initialization_expression], ...]) [:
parent_class_name(
args...), ...] {
}]
Here is a concrete example of giving arguments to an inherited base class:
Because base class constructors are executed before subclass constructors, the only local variables in the constructor that can be referenced are those declared in the subclass constructor declaration (if any). What this means is that if you declare local variables in the expressions giving base class arguments, these local variables are not accessible from the constructor body.
Classes inherited using the private
access modifier encapsulate the functionality (members, constants, methods, etc) of the parent class privately in the class hierarchy; that is, any access to the privately-inherited parent class's functionality from outside the class hierarchy will result in either a parse-time or runtime exception, depending on when the error is caught. When inheritance is declared private
, the inherted class is accessible in all child classes in the hierarchy.
Classes inherited using the private:internal
access modifier limit access to the inherited class to the child class; the inherited class in this case is not available in the rest of the class hierarchy but is limited to the directly inheriting child class.
Classes are inherited publically by default; public inheritance means that the parent class's functionality (members, constants, methods, etc) have the same visibility in the child class as they do in the parent class. For example, a public method in the parent class will also be public in the child class if the parent class is inherited publically. Respectively, private methods of publically-inherited parent classes will still be private in child classes.