Lua object oriented

Person = {b = 10};

Person.__index = Person;

function  Person.Init(name, age, year)

  local self = {};

  self._name = name or "";

  self._age  = age or "";

  self._year = year or "";

  setmetatable(self, Person);

  return self;

end

 

function Person:info()

  print("student name:"..self._name..";age:"..self._age..";year:"..self._year);

end

 

Student = {a = 10};

setmetatable(Student, Person);

Student.__index = Student;

function Student.Init()

  local self = Person.Init("helh2", 27, 1989);

  setmetatable(self, Student);

  --print((getmetatable(self) == Student));

  return self;

end

 

function Student:info()

  print("test");

end

 

p = Person.Init("helh", 26, 1990);

p:info();

 

s = Student.Init();

s: info ();

print(s.a);

print(s._age);

print(s.b);

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326571949&siteId=291194637