Getting "this" to work similar to "self" in Python

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 22 23:35:57 PDT 2015


On 2015-07-23 00:22, nurfz wrote:

I think you got overly complicated answers.

> I guess I'm confused as to why the D code isn't acting similar to the
> Python code in the sense that you would expect "this" to reference the
> "speed" property of the current instance and not statically reference
> the parent.  Am I having these issues because these attributes are being
> initialized statically?

No, it's not because they're statically initialized. It's because fields 
are not polymorphic.

> Would using constructors be the way to go about this? I suppose I'm just
> trying to find a way to implement fairly clean and intuitive object
> oriented inheritance that isn't crippled by getters/setters, is resolved
> at compile time, and doesn't impose any kind of runtime cost other than
> what you would assume is associated with fundamental level OOP.

Either you can set the value in the constructor or turn "speed" in to a 
method/property. I think it's easiest to set it in the constructor:

class Airplane : Vehicle
{
     this()
     {
         speed = 100;
     }
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list