www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - interfaces and properties

reply Roel Mathys <roel.mathys yucom.be> writes:
interface Foo
{
     int good();     // here I must define a method
}

class Bar : Foo
{
     int good() { return 15; }
/*
     // does not satisfy interface Foo
     int good;
     this() { good = 13; }
*/
}

void main()
{
     Bar b = new Bar();

     printf("%d\n",b.good);  // but I can access it as a property
}

too bad one can't define properties in an interface definition,
allowing properties but not allowing interfaces to define properties is 
a bit mmm ... I think it's not complete, I think properties are good, 
but something's amiss here

bye,
roel
May 19 2004
parent "Vathix" <vathixSpamFix dprogramming.com> writes:
"Roel Mathys" <roel.mathys yucom.be> wrote in message
news:c8fknc$1t0d$1 digitaldaemon.com...
 interface Foo
 {
      int good();     // here I must define a method
 }

 class Bar : Foo
 {
      int good() { return 15; }
 /*
      // does not satisfy interface Foo
      int good;
      this() { good = 13; }
 */
 }

 void main()
 {
      Bar b = new Bar();

      printf("%d\n",b.good);  // but I can access it as a property
 }

 too bad one can't define properties in an interface definition,
 allowing properties but not allowing interfaces to define properties is
 a bit mmm ... I think it's not complete, I think properties are good,
 but something's amiss here

 bye,
 roel

Properties are methods, not fields. Try this: interface Foo { int good(); } class Bar : Foo { int good() { return _good; } private int _good; this() { _good = 13; } } void main() { Bar b = new Bar(); printf("%d\n",b.good); }
May 19 2004