<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Метод класса __Set()]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15643&amp;type=atom" />
	<updated>2020-09-02T21:46:06Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=15643</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Метод класса __Set()]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141963#p141963" />
			<content type="html"><![CDATA[<p>Правильно так:<br /></p><div class="codebox"><pre><code>class Test
{
   __New(data_01, data_02) {
      this.data_01 := data_01
      this.data_02 := data_02
      this.storage := {}
   }
   
   __Get(key) {
      return this.storage[key]
   }
   
   __Set(key, value) {
      if !(key ~= &quot;i)^(data_(01|02)|storage)$&quot;) {
         this.storage[key] := value
         Return value
      }
   }
}

obj := new Test(&quot;data_01&quot;, &quot;data_02&quot;)

MsgBox,% obj.data_01
MsgBox,% obj.data_02

obj.data_03 := &quot;test&quot;
MsgBox,% obj.data_03</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-09-02T21:46:06Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141963#p141963</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Метод класса __Set()]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141962#p141962" />
			<content type="html"><![CDATA[<p>Но при этом значение не только помещается в &quot;storage&quot;, но ещё и дублируется в атрибуте &quot;obj&quot;. Соответственно, __Get(), при этом не вызывается, когда происходит обращение в &quot;MsgBox,% obj.data_03&quot;.<br /></p><div class="codebox"><pre><code>class Test
{
	__New(data_01, data_02) {
		this.data_01 := data_01
		this.data_02 := data_02
		this.storage := {}
	}
	
	__Get(key) {
		MsgBox, Call Get()
		return this.storage[key]
	}
	
	__Set(key, value) {
		If (key != &quot;storage&quot;)
			this.storage[key] := value
	}
}

obj := new Test(&quot;data_01&quot;, &quot;data_02&quot;)

MsgBox,% obj.data_01
MsgBox,% obj.data_02

obj[&quot;data_03&quot;] := &quot;test&quot;
MsgBox,% obj.data_03

for k, v in obj
	MsgBox,% &quot;from obj =&gt; &quot; k &quot; = &quot; v

for k, v in obj.storage
	MsgBox,% &quot;from obj.storage =&gt; &quot; k &quot; = &quot; v</code></pre></div>]]></content>
			<author>
				<name><![CDATA[KusochekDobra]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=33846</uri>
			</author>
			<updated>2020-09-02T19:43:29Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141962#p141962</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Метод класса __Set()]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141960#p141960" />
			<content type="html"><![CDATA[<div class="codebox"><pre><code>	__Set(key, value) {
		If (key != &quot;storage&quot;)
			this.storage[key] := value
	}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[KusochekBobra]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34401</uri>
			</author>
			<updated>2020-09-02T18:10:01Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141960#p141960</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: Метод класса __Set()]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141959#p141959" />
			<content type="html"><![CDATA[<p>Всем добра!</p><p>В справке сказано, что он вызывается для всякого атрибута, имя которого не было найдено при попытке присвоить ему значение. Но ведь на момент создания класса, конструктор __New() как раз занимается тем, что присваивает несуществующим атрибутам новые значения, а раз так, то следующий пример превращается в бесконечную рекурсию:<br /></p><div class="codebox"><pre><code>class Test
{
	__New(data_01, data_02) {
		this.data_01 := data_01
		this.data_02 := data_02
		this.storage := {}
	}
	
	__Get(key) {
		return this.storage[key]
	}
	
	__Set(key, value) {
		this.storage[key] := value
	}
}

obj := new Test(&quot;data_01&quot;, &quot;data_02&quot;)

MsgBox,% obj.data_01
MsgBox,% obj.data_02

obj.data_03 := &quot;test&quot;
MsgBox,% obj.data_03</code></pre></div><p>Подскажите пожалуйста, как правильно записать пример выше, чтобы создание атрибутов из __New() не превращалось в рекурсию.</p>]]></content>
			<author>
				<name><![CDATA[KusochekDobra]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=33846</uri>
			</author>
			<updated>2020-09-02T17:41:36Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141959#p141959</id>
		</entry>
</feed>
