Thread overview | |||||
---|---|---|---|---|---|
|
May 17, 2016 Abstract attribute ? | ||||
---|---|---|---|---|
| ||||
Hello, Why a attribute cannot be abstract ? How can I force the redefinition of an attribute if the class inherits from abstract class ? Example: ---------------- abstract class A { // Error: variable attr cannot be abstract protected abstract int attr; } class B : A { protected override int attr = 10; } |
May 17, 2016 Re: Abstract attribute ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | On Tuesday, 17 May 2016 at 16:52:01 UTC, Lucien wrote:
> Why a attribute cannot be abstract ?
Because it cannot be virtual and cannot be overridden. This is different than Python, but in line with other C-style languages (and the lower level implementation)
Use a property function instead to achieve this.
abstract class A
{
protected @property abstract int attr(); // a getter for attr
protected @property abstract attr(int); // a setter for attr, if desired
}
class B : A
{
override @property int attr() { return 0; } // implementation
}
|
May 17, 2016 Re: Abstract attribute ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 17 May 2016 at 16:58:30 UTC, Adam D. Ruppe wrote:
> On Tuesday, 17 May 2016 at 16:52:01 UTC, Lucien wrote:
>> Why a attribute cannot be abstract ?
>
> Because it cannot be virtual and cannot be overridden. This is different than Python, but in line with other C-style languages (and the lower level implementation)
>
> Use a property function instead to achieve this.
>
> abstract class A
> {
> protected @property abstract int attr(); // a getter for attr
> protected @property abstract attr(int); // a setter for attr, if desired
> }
>
> class B : A
> {
> override @property int attr() { return 0; } // implementation
> }
ok, thank you.
|
Copyright © 1999-2021 by the D Language Foundation