Development with Cocos code ide: Lua inheritance

Source: Internet
Author: User

When I write an edge error, I modify the edge and change the search. Then I made some changes. There may be errors, forgive me, and accept criticism.

 1 ---Base.lua 2  3 Base = {}                               4  5 Base.__index = Base 6 Base.value = nil 7  8 function Base:new(_value) 9     local _t = {}10     setmetatable(_t, Base)11     _t.value = _value12     return _t13 end
 1 ---Children.lua 2 require("Base") 3 Children = {} 4  5 setmetatable(Children, Base) 6 Children.__index = Children 7 Children.type = nil 8  9 function Childeren:new(_value,_type)10     local _c = Base:new(_value)11     setmetatable(_c, Children)12     _c.type = _type13     return _c14 end

Base. Lua is a base class, and children. Lua is a subclass that inherits base. Lua. You may get used to C ++.

 1 --- .h 2  3 class Base 4 { 5      public: 6           void setValue(_value) 7           int value;                       8 } 9 10 --- .cpp11 Base::setValue(_value)12 {13      value = _value;14 }15 16 main()17 {18     Base* b = new Base();19     cout << b.value << endl;20 }

Here, B is the same as the above _ T. I think it is the same and it is an object. _ t is of the table type,

_ T. value is like B. value.

Setdistribuable (children, base): sets the metadatabase of childeren to base,

Setretriable (_ c, children): Set the _ c meta table to children,

For example:

_ C: son children: Father base: Grandpa

The son wants to buy a toy. If: 1. He has money, buy it directly and get the toy.

2. If you have no money, search for your father's trouser pockets. If you have money, you will get the money and buy toys.

3. I have no money. I search for my father's trouser pockets. If I have no money, I search for my grandfather's trouser pockets. If I have money, I get money, I buy toys. If I have no money, I can only search for nil.

The same is true for class methods.

 

Development with Cocos code ide: Lua inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.