When a class implements an interface, it must include all methods and functions declared in the interface, else it won't compile. One thing worth noting is that access modifiers don't have any influence, if the caller works with the interface. For example all functions of the interface can be implemented as strict private members, but can still be called from another class if an instance of the interface is used.
Classes can implement more than one interface, as opposed to inheriting from more than one class Multiple Inheritance which isn't possible for Delphi classes. To achieve this, the name of all interfaces must be added comma-separated behind the base class. Of course, the implementing class must also define the functions declared by each of the interfaces. Interfaces can inherit from each other, exactly like classes do, too. An implementing class thus has to implement functions of the interface and all base interfaces.
This way, however, the compiler doesn't know that the implenting class also implements the base interface, it only knows of the interfaces that are explicitly listed. Or you could simply add the method to each sub class. Or you could use interfaces. Interfaces simply tidy up this situation. By defining an interface that groups these common methods, it sets a standard for all of the classes that implement the interface.
It makes it easier for the programmer to be disciplined in developing the application; all of the classes have an additional set of methods that are identical. And Delphi insists that all of the methods in an interface are implemented. Going back to the recyclable object scenario, each sub-class inherits common function from its particular parent class.
This describes the basic structure of the sub-class. It also inherits by implementation common function that cut across all classes. Like many ideas, an example is the best way to illustrate the concepts. We'll do it in stages to keep things as simple as possible.
This class is defined by default based on TObject since we have not specified a base class type. Here we set the two properties of the car : its name and age. The name is passed as a parameter, and we set a default age of 0. This class is a little different from the car class, enough to show that we could not have based the car on the bicycle or the bicycle on the car.
Now we'll define an interface that says whether a class object is recyclable:. Since the configuration screen in displayed modally, when it closes the next code ensures all IConfigChanged implementing forms are notified and ApplyConfigChange is called:.
The Supports function defined in Sysutils. The code iterates through the Screen. Forms collection of the TScreen object —all the forms currently displayed in the application. If a form Screen. Forms[cnt] supports the interface, Supports returns the interface for the last parameter parameter and returns true. Therefore, if the form implements the IConfigChanged, the icc variable can be used to call the methods of the interface as implemented by the form. Note, of course, that every form can have its own different implementation of the ApplyConfigChange procedure.
Any class you define in Delphi needs to have an ancestor. TObject is the ultimate ancestor of all objects and components. The above idea applies to interfaces also, the IInterface is the base class for all interfaces. This means that our IConfigChanged also has those 3 methods, but we have not implemented those.
When you want to implement an interface in a class that inherits from TObject, make sure your class inherits from TInterfacedObject instead. For example:. IUnknown is for COM. Actively scan device characteristics for identification. Use precise geolocation data. Select personalised content. Create a personalised content profile. Measure ad performance. Select basic ads. Create a personalised ads profile.
Select personalised ads. Apply market research to generate audience insights.
0コメント