On Thursday, 1 September 2016 at 11:09:18 UTC, slaid wrote:
I have a snippet:
How do I override this height field?
Thanks
The field height is not overridden. In C you have two "height".
Since your array is of type A[], map takes A.height.
abstract class A
{
int height = 0;
}
class B : A {}
class C : A
{
int height = 1;
}
void main()
{
writeln((new C).height); // prints 0, the height of C
writeln((cast(A)(new C).height); // prints 1, the height of A
}
Only methods are virtual. To solve the problem you can create a
virtual getter:
°°°°°°°°°°°°°°°°°°°°°°
abstract class A
{
int height();
}
class B : A
{
override int height(){return 0;}
}
class C : A
{
override int height(){return 1;}
}
°°°°°°°°°°°°°°°°°°°°°°
But since height is only a field you can just use the same
variable and set the value in the constructor (for example)
°°°°°°°°°°°°°°°°°°°°°°
abstract class A
{
int height;
}
class B : A
{
this(){height = 0;}
}
class C : A
{
this(){height = 1;}
}
°°°°°°°°°°°°°°°°°°°°°°