模板方法模式lua实现

--模板方法模式是通过把不变行为搬移到基类,去除之类中的重复代码来体现它的优势。
--下面以考试试卷为例,试题都是一样的,但是答案可能不一样

TestPaper = {}

function TestPaper:new(o)
	o = o or {}
	setmetatable(o,self)
	self.__index = self
	return o;
end

function TestPaper:TestQuestion1()
	print("孙悟空的师弟是谁?")
	print("A 猪七戒		B 猪八戒	          C 猪九戒")
	print("答案是:"..self:Answer1())
end

function TestPaper:Answer1()
	return ""
end

function TestPaper:TestQuestion2()
	print("猪八戒的师弟是谁?")
	print("A 沙和尚		B 沙道士	          C 沙井盖")
	print("答案是:"..self:Answer2())
end

function TestPaper:Answer2()
	return ""
end

function TestPaper:TestQuestion3()
	print("沙和尚的大师兄是谁?")
	print("A 孙中山		B 孙权		C 孙悟空")
	print("答案是:"..self:Answer3())
end

function TestPaper:Answer3()
	return ""
end

--小组A的试卷
GroupATestPaper = TestPaper:new()

function GroupATestPaper:Answer1()
	return "B"
end

function GroupATestPaper:Answer2()
	return "C"
end

function GroupATestPaper:Answer3()
	return "A"
end

--小组B的试卷
GroupBTestPaper = TestPaper:new()

function GroupBTestPaper:Answer1()
	return "A"
end

function GroupBTestPaper:Answer2()
	return "B"
end

function GroupBTestPaper:Answer3()
	return "C"
end


--小明是小组A的一员
print("********小明的试卷*********")
XiaoMingTestPaper = GroupATestPaper:new()
XiaoMingTestPaper:TestQuestion1()
XiaoMingTestPaper:TestQuestion2()
XiaoMingTestPaper:TestQuestion3()

--小红是小组B的一员
print("********小红的试卷*********")
XiaoHongTestPaper = GroupBTestPaper:new()
XiaoHongTestPaper:TestQuestion1()
XiaoHongTestPaper:TestQuestion2()
XiaoHongTestPaper:TestQuestion3()


输出结果是:

********小明的试卷*********
孙悟空的师弟是谁?
A 猪七戒  B 猪八戒 C 猪九戒
答案是:B
猪八戒的师弟是谁?
A 沙和尚  B 沙道士 C 沙井盖
答案是:C
沙和尚的大师兄是谁?
A 孙中山  B 孙权     C 孙悟空
答案是:A
********小红的试卷*********
孙悟空的师弟是谁?
A 猪七戒  B 猪八戒 C 猪九戒
答案是:A
猪八戒的师弟是谁?
A 沙和尚  B 沙道士 C 沙井盖
答案是:B
沙和尚的大师兄是谁?
A 孙中山  B 孙权     C 孙悟空
答案是:C

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值