Interface Inheritance

内容表

关于

An interface can be based on one or many other interfaces. This way the interface inherits the methods defined in the parent interfaces. An implementation of such an interface can re-implement the methods of the parent interfaces. It is also possible to use existing implementations of the inherited interfaces when the component is registered.

Parent interfaces are defined with the MAXON_INTERFACE_BASES attribute. The default parent interface is maxon::ObjectInterface .

Declaration

This example declares an interfaces that is directly based on maxon::ObjectInterface and an interface based on the previous interface:

// This example shows a base interface and a derived interface.

// --------------------------------------------------------------------- // Simple class to store a number. // --------------------------------------------------------------------- class NumberInterface : MAXON_INTERFACE_BASES (maxon::ObjectInterface) { MAXON_INTERFACE (NumberInterface, MAXON_REFERENCE_NORMAL , "net.maxonexample.interfaces.number" );

public : // --------------------------------------------------------------------- // Sets the stored number. // --------------------------------------------------------------------- MAXON_METHOD void SetNumber( maxon::Int number);

// --------------------------------------------------------------------- // Returns the stored number. // --------------------------------------------------------------------- MAXON_METHOD maxon::Int GetNumber() const ; };

// --------------------------------------------------------------------- // Advanced class to store and handle a number. // --------------------------------------------------------------------- class AdvancedNumberInterface : MAXON_INTERFACE_BASES (NumberInterface) { MAXON_INTERFACE (AdvancedNumberInterface, MAXON_REFERENCE_NORMAL , "net.maxonexample.interfaces.advancednumber_example" );

public : // --------------------------------------------------------------------- // Returns true if the number is even. // --------------------------------------------------------------------- MAXON_METHOD maxon::Bool IsEven() const ;

// --------------------------------------------------------------------- // Adds the given value to the stored value. // --------------------------------------------------------------------- MAXON_METHOD void Add( maxon::Int n); }; #include "interface_inheritance1.hxx"

// published object "NumberClass" presents the component class for an implementation of NumberInterface MAXON_DECLARATION ( maxon::Class<NumberRef> , NumberClass, "net.maxonexample.class.number" );

// published object "AdvancedNumber" presents the reference object for an implementation of AdvancedNumberInterface MAXON_DECLARATION (AdvancedNumberRef, AdvancedNumber, "net.maxonexample.advancednumber" ); #include "interface_inheritance2.hxx"

Implementation

Within the functions of an implementation it is possible to access the inherited functions using super:

// This example implements an interface which inherits from another interface.

// implementation of AdvancedNumberInterface class AdvancedNumberImpl : public maxon::Component <AdvancedNumberImpl, AdvancedNumberInterface> { // add the component "NumberClass" as an implementation of the inherited interface "NumberInterface". MAXON_COMPONENT ( NORMAL , NumberClass); public : // overridden function of NumberInterface MAXON_METHOD void SetNumber( maxon::Int number) { // detect properties for fast access later _even = false ;

// check parity if (number % 2 == 0) _even = true ;

// call "SetNumber()" of the implementation defined with MAXON_COMPONENT() super.SetNumber(number); }

// implementation of AdvancedNumberInterface functions MAXON_METHOD maxon::Bool IsEven() const { return _even; } MAXON_METHOD void Add( maxon::Int n) { // obtain number value from super class maxon::Int number = super.GetNumber(); number += n;

// store number value by calling the implementation // of SetNumber() in this class self .SetNumber(number); } private : maxon::Bool _even; };

The result class can be used like this:

// This example shows the usage of a simple reference class. Both // functions of the base interface and a derived interface are used.

// create object const AdvancedNumberRef number = AdvancedNumber();

// call overridden function number.SetNumber(100);

// use functions of derived interface if (number.IsEven()) DiagnosticOutput ( "Number is even" ); number.Add(1); if (!number.IsEven()) DiagnosticOutput ( "Number is not even" );

// use function of base interface DiagnosticOutput ( "Number: @" , number.GetNumber());

组件

Additional components are typically implementations of the inherited interfaces and define the specific behaviour of the registered component. They can be added using the MAXON_COMPONENT() attribute or by implementing maxon::ComponentRoot::ConfigureClass().

延伸阅读

maxon::ComponentWithBase
定义: objectbase.h:2472
maxon::Class
定义: objectbase.h:681
MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
定义: apibase.h:177
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
MAXON_DECLARATION
#define MAXON_DECLARATION(T, Name, id)
定义: module.h:797
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
SCULPTBRUSHMODE::NORMAL
@ NORMAL
Samples the surface as the user moves over it the SculptObject and returns a new hit point and normal...
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
MAXON_REFERENCE_NORMAL
#define MAXON_REFERENCE_NORMAL(DUMMY)
定义: interfacebase.h:957

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1