交通工具类
- Vehicle(速度(speed) / 通过里程数求时间(run))
- 鸣笛(多态)
local Vehicle = {}
function Vehicle:new(speed)
local obj = {}
obj.speed = speed or 20
setmetatable(obj,self)
self.__index = self
return obj
end
function Vehicle:run(miles)
return miles / self.speed
end
function Vehicle:speak()
print("Vehicle:speak")
end
汽车类Car
local Car = Vehicle:new()
function Car:ZaiRen()
print("ZaiRen")
end
function Car:speak()
print("滴滴滴")
end
local car = Car:new(100)
print(car:run(200))
car:ZaiRen()
car:speak()
卡车类Truck
local Truck = Vehicle:new()
function Truck:ZaiHuo()
print("ZaiHuo")
end
function Truck:speak()
print("哒哒哒")
end
local truck = Truck:new()
truck:ZaiHuo()
truck:speak()