Weird behavior of "this" in a subclass, I think?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 15 18:05:42 PDT 2015


On Thursday, 16 July 2015 at 00:39:29 UTC, H. S. Teoh wrote:
>>
> If you want to simulate overriding of class variables, you can 
> use a @property method instead:
>
> 	class Animal {
> 		@property string voice() { return "Wah!"; }
> 		void speak() { writeln(voice); }
> 	}
>
> 	class Dog : Animal {
> 		override @property string voice() { return "Whoof!"; }
> 	}
>
>
Alternatively:

     class Animal {
          protected string voice;
          void speak() { writeln(voice); }
      }

      class Dog : Animal {
          this() { voice = "Whoof!"; }
      }




More information about the Digitalmars-d-learn mailing list