自定义脚本语言引擎开发纪实 -类的继承(类似python)
程序员文章站
2022-04-10 15:35:29
...
类的继承
单继承例子
class stature
func __init__(self, height)
self.height = height
end
func __del__(self)
end
func GetHeight(self)
return self.height
end
endclass
class child(stature)
sum=0
school="大二班"
func __init__(self, name, age, height)
self.name = name
self.age = age
child.sum = child.sum + 1
//调用基类构造函数
stature.__init__(self, height)
end
func __del__(self)
child.sum = child.sum - 1
end
func GetSchool()
return child.school
end
func GetSum()
return child.sum
end
func GetNameAndAge(self)
return self.name, self.age
end
endclass
测试
yew=child("耀耀", 6, 120)
js.debug.log("log\tname:" + yew.name + "\tage:" + yew.age + "\theight:" + yew.height)
结果:
[11588] log name:耀耀 age:6 height:120
多继承(继承类似python)
class stature
func __init__(self, height)
self.height = height
end
func __del__(self)
end
func GetHeight(self)
return self.height
end
endclass
//child继承自stature
class child(stature)
sum=0
school="大二班"
func __init__(self, name, age, height)
self.name = name
self.age = age
child.sum = child.sum + 1
//调用基类构造函数
stature.__init__(self, height)
end
func __del__(self)
child.sum = child.sum - 1
end
func GetSchool()
return child.school
end
func GetSum()
return child.sum
end
func GetNameAndAge(self)
return self.name, self.age
end
endclass
class gender
boys = 0
girls = 0
func __init__(self, gender_type)
self.gender_type = gender_type
if(gender_type == "男")
then
gender.boys = gender.boys + 1
elseif(gender_type == "女")
then
gender.girls = gender.girls + 1
end
end
func __del__(self)
if(self.gender_type == "男")
then
gender.boys = gender.boys - 1
elseif(self.gender_type == "女")
gender.girls = gender.girls - 1
end
end
func GetGender(self)
return self.gender_type
end
func GetBoysAndGirlsSum()
//返回男孩数,女孩数,总人数
return gender.boys, gender.girls, gender.boys + gender.girls
end
endclass
//多继承
class student(child, gender)
func __init__(self, name, age, height, gender_type)
//调用基类的构造函数, 类似python
child.__init__(self, name, age, height)
gender.__init__(self, gender_type)
end
func __del__(self)
//调用基类的析构函数
child.__del__(self)
gender.__del__(self)
end
endclass
测试
xiaoming=student("小明", 8, 130, "男")
yew=student("耀耀", 6, 120, "男")
xiaohua=student("小花", 8, 135, "女")
xiaoli=student("小丽", 6, 117, "女")
xiaogang=student("小刚", 7, 123, "男")
xiaoya=student("小雅", 7, 120, "女")
js.debug.log("log\tname:" + xiaoming.name + "\tage:" + xiaoming.age + "\t身高" + xiaoming.height + "\t性别" + xiaoming.gender_type)
js.debug.log("log\tname:" + yew.name + "\tage:" + yew.age + "\t身高" + yew.height + "\t性别" + yew.gender_type)
js.debug.log("log\tname:" + xiaoli.name + "\tage:" + xiaoli.age + "\t身高" + xiaoli.height + "\t性别" + xiaoli.gender_type)
js.debug.log("log\t学校:" + child.school + "共有学生:" + child.GetSum() + "名")
boys, girls, sum = gender.GetBoysAndGirlsSum()
js.debug.log("log\t男孩:" + boys + "\t女孩:" + girls + "\t共" + sum + "人")
运行结果
[10288] log name:小明 age:8 身高130 性别男
[10288] log name:耀耀 age:6 身高120 性别男
[10288] log name:小丽 age:6 身高117 性别女
[10288] log 学校:大二班共有学生:6名
[10288] log 男孩:3 女孩:3 共6人
上一篇: 关于.NET异常处理的思考