A class is a data type that can contain fields, constants, methods, properties, and signals. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.
The simplest class declaration looks like this:
class ClassName { <class-member> }
As class types support inheritance, you can specify a base class you want to derive from:
class ClassName : BaseClassName { <class-member> }
It's recommended that you derive all your classes directly or indirectly from GLib.Object, unless you have a strong reason not to. Some class features are not supported for classes not deriving from GLib.Object.
Classes cannot have multiple base classes, however they may implement multiple interfaces:
class ClassName : BaseClassName, FirstInterfaceName, SecondInterfaceName { <class-member> }
You may optionally specify an accessibility modifier. Classes support public
and private
accessibility and default to private if you don't specify one. Public classes may be accessed from outside the library or application they are defined in.
public class ClassName { <class-member> }
The abstract
modifier may be placed between the optional accessibility modifier and the class name to define an abstract class. An abstract class cannot be instantiated and is used as a base class for derived classes.
abstract class ClassName { <class-member> }
The static
modifier may be placed between the optional accessibility modifier and the class name to define a static class. A static class cannot be instantiated and may not have a base class. It can also not be used as a base class for derived classes and may only contain static members. Static classes are implicitly abstract, you may not use both modifiers, abstract
and static
, in the same class declaration.
static class ClassName { <class-member> }
You may optionally prefix the class name with a namespace name. This places the class in the specified namespace without the need for a separate namespace declaration.
class NamespaceName.ClassName { <class-member> }
Documentation
Documentation
property-declaration: [ access-modifier ] [ member-modifiers ] type identifier { accessor-declarations [ default-value ] } ; accessor-declarations: get-accessor [ set-accessor ] set-accessor [ get-accessor ] get-accessor: [ access-modifier ] get ; [ access-modifier ] get { statement-list } set-accessor: [ access-modifier ] set [ construct ] ; [ access-modifier ] set [ construct ] { statement-list } default-value: default = expression ;
The signal system allows objects to emit signals that can be handled by user-provided signal handlers.
signal-declaration: [ access-modifier ] signal return-type identifier ( [ parameter-list ] ) ;