007Visual Basic模板方法模式

1.模板及子类 

'模板类
Public MustInherit Class TestPaper
    Public Sub TestQuestion1()
        Console.WriteLine("杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可是【】a.球磨铸铁 b.马口铁 c.高速合成金钢 d.碳素纤维")
        Console.WriteLine("答案:" & Answer1())
    End Sub
    Public Sub TestQuestion2()
        Console.WriteLine("杨过、程英、陆无双铲除了情花,造成【】a.使这种植物不再害人 b.使一个珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化")
        Console.WriteLine("答案:" & Answer2())
    End Sub
    Public Sub TextQuestion3()
        Console.WriteLine("蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】a.阿司匹林 b. 牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对")
        Console.WriteLine("答案:" & Answer3())
    End Sub
    Protected MustOverride Function Answer1()
    Protected MustOverride Function Answer2()
    Protected MustOverride Function Answer3()
End Class


Public Class TestPaperA
    Inherits TestPaper
    Protected Overrides Function Answer1() As Object
        Return "b"
    End Function

    Protected Overrides Function Answer2() As Object
        Return "c"
    End Function

    Protected Overrides Function Answer3() As Object
        Return "a"
    End Function
End Class

Public Class TestPaperB
    Inherits TestPaper
    Protected Overrides Function Answer1() As Object
        Return "c"
    End Function

    Protected Overrides Function Answer2() As Object
        Return "a"
    End Function

    Protected Overrides Function Answer3() As Object
        Return "a"
    End Function

End Class

2.客户端

Module Module1

    Sub Main()
        Console.WriteLine("学生甲抄的试卷:")
        '声明为父类,利用多态
        Dim StudentA As TestPaper = New TestPaperA
        StudentA.TestQuestion1()
        StudentA.TestQuestion2()
        StudentA.TextQuestion3()
        Console.WriteLine()

        Console.WriteLine("学生乙抄的试卷:")
        Dim StudentB As TestPaper = New TestPaperB
        StudentB.TestQuestion1()
        StudentB.TestQuestion2()
        StudentB.TextQuestion3()

        Console.Read()
    End Sub

End Module

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值