Question on Interface.

JS js.mdnq at gmail.com
Wed Aug 7 07:23:17 PDT 2013


On Wednesday, 7 August 2013 at 07:11:58 UTC, SteveGuo wrote:
> I like the concept of interface, It enforces its successors to 
> implement some necessary methods.
>
> Please take a look at the following example:
>
> device.d
> ---------------------------------
> interface device
> {
> // I want the three methods implemented by its successors
>     void PowerOn();
>     void PowerOff();
>     void Reset();
>
>     string signature; // This is not allowed in D, but every 
> device should has a signature.
> }
>
> Why it doesn't support non-static members?
> Actually in many cases, we need an interface which needs 
> enforce its successors to implement necessary methods, 
> meanwhile it needs has its own non-static members.


Use properties. Interfaces cannot require fields but can require 
methods. A property is a getter and setter method that wraps a 
field.

So instead ing string signature; use @property string signature();

Then for all practical purposes you can treat it as a field.



More information about the Digitalmars-d mailing list