Thursday, 15 August 2013

oop - Lua objects tables leaking between object instances, but not records -


I'm trying to learn Lua, so hopefully this is an easy question to answer. The following code does not work Variable shells between all instances of classes leak

  - [[- reference class. -]] - CxBR_Context = {} - Reference name CxBR_Context.name = "Reference name is not set" - Child reference list CxBR_Context.childContexts = {} - Create a new instance of a reference class function CxBR_Context: New Object (Object) = Object or {} - If the user does not provide a setetable (object, self), then the object does not provide itself .__ index = self return Object End - Child reference function CxBR_Context: AddChildContext (reference) table.insert (self .childContexts, Context ("Add Child Reference"). Context.name .. "to" ..self.name) end - [[reference 1 class. References CxBR_Context -]] - Reference 1 = CxBR_Context: New {name = "Context1"} - [[- Reference 1A class. Inherits CxBR_Context -]] - Context1A = CxBR_Context: new {name = "Context1A"} - [[- - reference 2 square] Inheritance CxBR_Context -]] - Contex 2 = CxBR_Context: new {name = "Context2"} - [[- - Test -]] - Reference 1 = Reference 1: New () - Example of context 1 class, make an example (reference 1.name .. "is" .. table .getn (context1.childContexts) .. "children") Context2 = Context2: New () - Create an example of reference 2 square prints (reference 2.name .. "is" .. table.getn (context2.childContexts) .. "children") context1A = Context1A: new () - reference Make an example of 1a class print (context1A.name .. ".. .. table.get N (context1A.childContexts) .. "Children") Reference 1: AddChildContext (context1A) - Add Reference to the Child as Reference 1A 1. Print (Reference 1.name .. "is" .. table.getn (context1. ChildContexts.) "Children") - The result is fine, 1 child has a print (reference 2.name .. "is" .. table.getn (context2.childContexts) .. "children") - why thin return 1, 0 should be   

I can fix my problem by changing the constructor function:

  - child reference list - CxBR_Context.childContexts = {} - a Reference class function CxBR_Context Create a new example: Create a new object (object) = object or {childContexts = {}} - if the user creates a setmable (object, self) self.__ index = self return object and   < P> does not provide my questions:  
  1. Is there any cleaner way to declare class variables like the first instance, so I do not need to include it in constructor?
  2. Why does CxBR_Context.name work, but do not table CxBR_Context.childContexts?

    1. No, I do not think so. You want each child to make reference items by creating your own child-related field.

    2. This works because you pass a table containing a name field for a new field. What you did here:

      Reference 1 = CxBR_Context: Create a new {name = "Context1"}

      • Field "with name" Set to "Contact 1"
      • Pass the table to the manufacturer
      • Assign a metaTable (original CxBR_Context table) to the table created in the first step [in the constructor].

        So basically, when you call Context1.name, you retrieve a field from the Context 1 table, which is the beginning of the constructor But when you index it with childContext and no such field exists (because you have created the "name" field only at the constructor stage), Lula appears in the __ Indac Table, which is CxBR_Context. The table is normal for all reference area objects.

        Edit:

          object = object or {childContexts = {}} - create an object if the user does not provide one   

        Actually, this This will not work if you supply your table like this:

          Context1 = CxBR_Context: new {name = "Context1"}   

        This will work only if your object's availability is zero, then it is if you call the constructor with the parameter itself.

No comments:

Post a Comment