【接口定义】接口就是指只包含虚成员的虚类。
(1)虚类,表明了接口是不能够被直接实例化的。也就是说,接口只是一个抽象概念。比如我们说车可以跑,人可以跑,马可以跑。我们可以看到实例化的车、人、马。但是我们可以定义一个“会跑的物质”。它可以是车,也可以使人、马,但是我们却不能说“这个东西就是一个会跑得物质,但是它并不是车、人或者马。”
(2)只包含虚成员,表明了接口只是说明了它具有什么样的功能,可以提供什么样的信息。但是这些功能和信息究竟是什么,如何提供我们无法得知。就像是“会跑的物质”,我们知道它可以跑,但是具体他怎么跑我们就不知道了。之所以说接口可以部分替代多继承,就是因为VB.NET只允许一个类继承自另一个,且只能是这个类;但是一个类可以实现一个或多个接口。由于接口不实现成员,只声名成员,所以也就不存在多继承的路径问题了。
什么情况下使用接口:
当我们面临一个问题,就是我们有一个功能,它需要操作不同的类的实例去完成一个目的相同的方法的时候,我们就可以把这些目的相同的方法作为接口来实现。这里我们就用橘子苹果的例子举例:
【橘子苹果实例】
功能分析:
有个只进不出的盒子,水果有两种苹果和香蕉,原始重量分别是50、30。放入盒中,水分丢失,重量减轻,分别减轻4、3,直到达到自身3/5不再轻。
盒子功能:
1、一天前的总重量
4、输出一天盒中水果减轻的总重量
5、当前的总重量
这里主要用到的方法就是统计当前重量、统计减少重量。橘子和苹果都用到了这两个相同方法实现相同的目的。所以在这个例子中我们就把统计当前重量、统计减少重量这两个方法封装成了接口。这个例子是让苹果和橘子类去完成此接口(目的相同的方法),以实现软件最终的功能目的。
【代码实现】
2、Apple类实现接口
- Implements Fruit
- Private mvarCurWeight As Double '定义当前重量的变量
- Private MvarTotalWeight As Double '定义总重量的变量
- Private Sub Class_Initialize() '类的初始化
- '苹果的初始化重量为50,当前的重量也是50
- MvarTotalWeight = 50
- mvarCurWeight = MvarTotalWeight
- End Sub
- Private Property Get Fruit_CurWeight() As Double '写入苹果当前的重量
- Fruit_CurWeight = mvarCurWeight
- End Property
- Private Function Fruit_ReduceWeight() As Double '苹果的重量每天减少4,直到是原始重量的3/5
- Dim oldWeight As Double '定义旧重量的变量
- oldWeight = mvarCurWeight '将减少前的重量值赋给旧重量
- mvarCurWeight = mvarCurWeight - 4 '苹果减重4
- If (mvarCurWeight < MvarTotalWeight * 3 / 5#) Then
- mvarCurWeight = MvarTotalWeight * 3 / 5 '苹果最低重量的整个的3/5
- End If
- Fruit_ReduceWeight = oldWeight - mvarCurWeight '返回值是减少的重量=旧重量-新重量
- End Function
3、Orange类实现接口
- Implements Fruit
- Private mvarCurWeight As Double '定义当前重量的变量
- Private MvarTotalWeight As Double '定义总重量的变量
- Private Sub Class_Initialize() '类的初始化
- '橘子的初始化重量为50,当前的重量也是50
- MvarTotalWeight = 30
- mvarCurWeight = MvarTotalWeight
- End Sub
- Private Property Get Fruit_CurWeight() As Double '写入橘子当前的重量
- Fruit_CurWeight = mvarCurWeight
- End Property
- Private Function Fruit_ReduceWeight() As Double '橘子的重量每天减少3,直到是原始重量的3/5
- Dim oldWeight As Double '定义旧重量的变量
- oldWeight = mvarCurWeight '将减少前的重量值赋给旧重量
- mvarCurWeight = mvarCurWeight - 3 '橘子减重4
- If (mvarCurWeight < MvarTotalWeight * 3 / 5#) Then
- mvarCurWeight = MvarTotalWeight * 3 / 5 '橘子最低重量的整个的3/5
- End If
- Fruit_ReduceWeight = oldWeight - mvarCurWeight '返回值是减少的重量=旧重量-新重量
- End Function
4、 FruitBox类是功能实现的一个类,此软件的功能全部都是在FruitBox类中的实现。
- Private mcol As Collection '定义集合,元素是水果
- Public Sub AddFruit(aFruit As Fruit) '向盒子中添加一个水果
- mcol.Add aFruit
- End Sub
- Public Function TotalFruitWeight() As Double '计算当前盒子水果的总重量
- Dim aFruit As Fruit
- Dim total As Double
- total = 0
- For Each aFruit In mcol
- total = total + aFruit.CurWeight
- Next
- TotalFruitWeight = total '函数返回值是所有的总重量
- End Function
- Public Function PassOneDay() As Double '经过一天后盒子的重量
- Dim aFruit As Fruit
- Dim total As Double
- total = 0
- For Each aFruit In mcol
- total = total + aFruit.ReduceWeight '所有水果减轻的重量
- Next
- PassOneDay = total '函数返回值是所有的总重量
- End Function
- Public Function NumOfApples() As Long '盒子里面苹果的个数
- Dim aFruit As Fruit
- Dim count As Long
- count = 0
- For Each aFruit In mcol
- If (TypeName(aFruit) = "Apple") Then
- count = count + 1
- End If
- Next
- NumOfApples = count
- End Function
- Public Function NumOfOranges() As Long '盒子里面橘子的个数
- Dim aFruit As Fruit
- Dim count As Long
- count = 0
- For Each aFruit In mcol
- If (TypeName(aFruit) = "Orange") Then
- count = count + 1
- End If
- Next
- NumOfOranges = count
- End Function
- Private Sub Class_Initialize() '初始化建立集合
- Set mcol = New Collection
- End Sub
- Private Sub Class_Terminate() '清理内存,释放集合对象
- ReleaseFruits
- Set mcol = Nothing
- End Sub
- Private Sub ReleaseFruits() '释放对象
- Dim aFruit As Fruit
- For Each aFruit In mcol
- Set aFruit = Nothing
- Next
- End Sub
5、主窗体实例化,主窗体通过调用FuritBox里的具体功能方法,来实现主窗体具体操控功能。然后将整个系统运算的结果呈现到主窗体上Text中,供用户查看。
- Dim Box As FruitBox '定义一个盒子类型
- Private Sub cmdApple_Click() '添加一个苹果
- Box.AddFruit New Apple
- End Sub
- Private Sub cmdOrange_Click() '添加一个橘子
- Box.AddFruit New Orange
- End Sub
- Private Sub cmdPassDay_Click() '过了一天统计
- Dim str As String '定义字符串变量
- Dim Old As Double '定义旧重量变量
- Dim Reduce As Double '定义损失重量变量
- Dim Now As Double '定义新重量变量
- Old = Box.TotalFruitWeight() '调用FruitBox类的TotalFruitWeight方法
- str = "一天前总重量有:" & Old
- LstShow.AddItem str
- str = "苹果有:" & Box.NumOfApples() '调用FruitBox类的NumOfApples方法
- LstShow.AddItem str
- str = "橘子有:" & Box.NumOfOranges() '调用FruitBox类的NumOfOranges方法
- LstShow.AddItem str
- Reduce = Box.PassOneDay '调用FruitBox类的PassOneDay方法
- str = "一天损失重量:" & Reduce
- LstShow.AddItem str
- Now = Box.TotalFruitWeight() '调用FruitBox类的TotalFruitWeight方法
- str = "当前重量:" & Now
- LstShow.AddItem str
- LstShow.AddItem ""
- End Sub
- Private Sub Form_Load() '创建一个盒子对象
- Set Box = New FruitBox
- End Sub
- Private Sub Form_Unload(Cancel As Integer) '释放一个盒子对象
- Set Box = Nothing
- End Sub