Тема: AHK: Свой get\set при доступе к ассоциированным массивам внутри класса
Общий привет!
Вот модифицированный код и справки:
red := new Color(0xff0000) ; but not here
red.R := 30 ; how to do some special here??
cyan := new Color(0), cyan.G := 255, cyan.B := 255
MsgBox % "red: " red.R "," red.G "," red.B " = " red.RGB
MsgBox % "cyan: " cyan.R "," cyan.G "," cyan.B " = " cyan.RGB
class Color
{
__New(aRGB)
{
this.init := 1
this.RGB := aRGB
this.init := 0
}
static Shift := {R:16, G:8, B:0}
__Get(aName)
{
if (Color.init1 != 0)
{
MsgBox, % A_ThisFunc . aName
; NOTE: Using this.Shift here would cause an infinite loop!
shift := Color.Shift[aName] ; Get the number of bits to shift.
if (shift != "") ; Is it a known property?
return (this.RGB >> shift) & 0xff
; NOTE: Using 'return' here would break this.RGB.
}
}
__Set(aName, aValue)
{
if (Color.init1 != 0)
{
MsgBox, % A_ThisFunc . aName . "-" . aValue
if ((shift := Color.Shift[aName]) != "")
{
aValue &= 255 ; Truncate it to the proper range.
; Calculate and store the new RGB value.
this.RGB := (aValue << shift) | (this.RGB & ~(0xff << shift))
; 'Return' must be used to indicate a new key-value pair should not be created.
; This also defines what will be stored in the 'x' in 'x := clr[name] := val':
return aValue
}
; NOTE: Using 'return' here would break this.stored_RGB and this.RGB.
}
}
; Meta-functions can be mixed with properties:
RGB[] {
get
{
; Return it in hex format:
return format("0x{:06x}", this.stored_RGB)
}
set
{
; MsgBox, % "Set RGB" . this.stored_RGB
return this.stored_RGB := value
}
}
init[]
{
get
{
return this.stored_init
}
set
{
return this.stored_init := value
}
}
}
Объясню по-простому, в свой класс добавляются ассоциированные массивы. Получается так:
myClass.someArray.arrayKey := "testValue" ; set
Msgbox, % myClass.someArray.arrayKey ;get
Мне нужно выполнять еще некоторые действия, когда я обращаюсь к элементам класса как выше, но не при создании самого класса.