About Static/class variables inherited by subclasses Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

About Static/class variables inherited by subclasses

21 Sep 2021, 14:13

This line is from the docs: ( https://lexikos.github.io/v2/docs/Objects.htm#Custom_Classes_staticvar )
Static/class variables belong to the class itself, but their values can be inherited by subclasses.
I don't understand what that means.
I've tried things similar to this with no luck:

Code: Select all

class Example
{
	static Inherit := "Inherit?"
	class Subexample
	{
	}
}
MsgBox Example.Subexample.Inherit
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: About Static/class variables inherited by subclasses

21 Sep 2021, 14:24

I believe that "subclass" is referring to derived classes, not "nested classes".
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: About Static/class variables inherited by subclasses

21 Sep 2021, 14:32

Maybe, I think my English might be a bit lacking sometimes.

Do you mean as in using extends? But I'm not sure that would need clarification since extends are what they are :? (I think)

Edit: Maybe there are less obvious cases than that that I'm missing right now?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: About Static/class variables inherited by subclasses  Topic is solved

21 Sep 2021, 17:37

u have to read the entire thing:
Static/class variables belong to the class itself, but their values can be inherited by subclasses. They are declared like instance variables, but using the static keyword:

Code: Select all

static ClassVar := Expression
These declarations are evaluated only once, when the class is initialized. A static method named __Init is automatically defined for this purpose.

Each declaration acts as a normal property assignment, with the class object as the target. Expression has the same interpretation as for instance variables, except that this refers to the class itself.

To assign to a class variable anywhere else, always specify the class object; for example, ClassName.ClassVar := Value. If a subclass does not own a property by that name, Subclass.ClassVar can also be used to retrieve the value; so if the value is a reference to an object, subclasses will share that object by default. However, Subclass.ClassVar := y would store the value in Subclass, not in ClassName.

Code: Select all

class Example
{
	static Inherit := "Inherit?"
}

class Subexample extends Example
{
}

MsgBox Subexample.Inherit ; "Inherit?"

Subexample.Inherit := 'something else'
MsgBox( Example.Inherit ; "Inherit?", NOT 'something else'
'`n' Subexample.Inherit ; 'something else'
)
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: About Static/class variables inherited by subclasses

22 Sep 2021, 08:43

Thanks, I got stuck at the beginning and had troubles going on. As you say that last line makes gives it a bit more meaning.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Archimede, Rohwedder, songdg and 23 guests