Game Development Reference
In-Depth Information
vehicle:isVehicle()
car = {}
function car:name()
print("Car")
end
car:name()
carTable= { __index = vehicle}
setmetatable(car, carTable)
car:isVehicle()
car:name()
vehicle:name()
In terms of an object-oriented language, a
virtual
(or
abstract
) class has functions that are common,
and each object that inherits from this class (see Figure
6-1
) has all the functions of the class.
Further, you can add more functions that are specific for the new inherited class, as shown in
Figure
6-2
.
Figure 6-1.
An abstract or virtual (parent) class
Figure 6-2.
An inherited or child class
In the preceding code example, we create a virtual class called
vehicle
and add the function
isVehicle
as a member function of the class; the function prints
Yes
when called. Then we create a
new object called
car
, which has a member function called
name
that prints
Car
when called. Let's
revisit that piece of code:
vehicle = {}
function vehicle:isVehicle()
print("Yes!")
end