Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Class declaration - I usually group all of these together under a comment labeled "Classes" or "Objects"
- -- You also declare any arguments here.
- function TestClass(arg1, arg2)
- -- Beginning of "values" - initialize variables and functions of the object here
- local values = {
- -- some test variables
- val1 = 5,
- val2 = arg1 + arg2,
- val3 = arg1 - arg2,
- -- a test function
- mix = function(tc1, tc2)
- local arg1 = tc1.val2 + tc2.val2
- local arg2 = tc1.val3 + tc2.val3
- return TestClass(arg1, arg2)
- end
- }
- -- put more complex constructor code here
- if math.random(1, 6) > 3 then
- values.val4 = 4
- else
- values.val4 = 1
- end
- -- ALWAYS return values
- return values
- end
- -- to create a new TestClass object
- a = TestClass(5, 2)
- b = TestClass(8, 6)
- -- Assuming RFC 1149.5 compliance, the values of the two test classes should be...
- -- a.val1 = 5, a.val2 = 7, a.val3 = 3, a.val4 = 4
- -- b.val1 = 5, b.val2 = 14, b.val3 = 2, b.val4 = 4
- c = a:mix(b)
- -- c.val1 = 5, c.val2 = 21, c.val3 = 5, c.val4 = 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement