سئوال شی گرایی در لوا

SajjadKhati

کاربر فعال <A href="http://forum.majidonline.com/f
سلام
کسی مشکل این کد رو میدونه که چرا دو شی Obj1 و Obj2 ، مستقل کار نمیکنن؟
وگرنه نمیدونین توی کجا باید بپرسم؟ :

کد:
MyArray = {x=10, y=20, 30}


function MyArray.SetFunc(Arg1, b)
Arg1.x = b
end


function MyArray.GetFunc(Arg1)
return Arg1.x
end


function MasterFunc()
return MyArray
end


Obj1 = MasterFunc()
Obj1:SetFunc( 999)
Obj2 = MasterFunc()
Obj2:SetFunc( "salam")


x = Obj1:GetFunc()
y = Obj2:GetFunc()


Dialog.Message("Notice", x.."\n"..y, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);

ولی کد زیر درسته . کد بالا چرا کار نمیکنه . نمیدونم با اونکه هر دو یه ساختار رو دارن :

کد:
function newRGB(r,g,b)
o ={
    red=r;
    green=g;
    blue=b;
    name='';
    x=100;
    setName = function(self,name)
      self.name=name;
    end;
    getName = function(self)
      return self.name;
    end;
    tostring = function(self)
      return self.name..' = {'..self.red..','..self.green..','..self.blue..'}'
    end
  }
  return o
end






blue = newRGB(0,0,255);
blue:setName('blue');


yellow = newRGB(255,255,0);
yellow:setName('yellow');


print(yellow:tostring());
print(blue:tostring());
 

SajjadKhati

کاربر فعال <A href="http://forum.majidonline.com/f
آو
تابع ها رو داخل تابع اصلی میبرم ، درست میشه . چه فرقی میکنه مگه؟ :

کد:
function MasterFunc()
MyArray = {x=10, y=20, 30}
	function MyArray.SetFunc(Arg1, b)
	Arg1.x = b
	end


	function MyArray.GetFunc(Arg1)
	return Arg1.x
	end


return MyArray
end


Obj1 = MasterFunc()
Obj1:SetFunc( 999)
Obj2 = MasterFunc()
Obj2:SetFunc( "salam")


x = Obj1:GetFunc()
y = Obj2:GetFunc()


Dialog.Message("Notice", x.."\n"..y, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
 

جدیدترین ارسال ها

بالا