程序员

LUA OOP

2017-04-24  本文已影响0人  庄什么庄

-- Class of ParentClass

Animals = {x = 0, y = 0};

Animals.__index = Animals;

-- Constructor

function Animals:New(x, y)

local self = {};

setmetatable(self, Animals);

self.x = x;

self.y = y;

return self;

end

function  Animals:Fly()

-- body

print("animal fly");

end

-- print(Animals:New(1, 5).x);

Duck = {z = 0};

setmetatable(Duck, Animals);

Duck.__index = Duck;

function Duck:New(x, y, z)

-- body

local self = {};

self = Animals:New(x, y);

setmetatable(self, Duck);

self.z = z;

return self;

end

-- function Duck:Fly()

-- -- body

-- print("duck fly");

-- end

function Cry( )

-- body

print("duck cry");

end

animal = Animals:New(1, 5);

duck = Duck:New(3, 88);

print(animal:Fly());

print(duck.Fly());

上一篇下一篇

猜你喜欢

热点阅读