你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
VB.Net中文教程(三) 继承与封装性(4)
 

Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click( ByVal sender As Object,
ByVal e As System.EventArgs)
Dim p1 As New Teacher("David", 25)
p1.Show()
p1.modify("Kent Smith", 45)
p1.Show()
End Sub
End Class


此程序输出:
Name: David Age: 25
Name: Kent Smith Age: 45

此时﹐子孙类别之函数能呼叫modifyAge()﹐但家族外之类别仍不可呼叫它。总结归纳为简单规则﹕
◎如果允许任何类别使用﹐就宣告为Public。
◎如果允许子类别使用﹐就宣告为Protected 。
◎如果允许本类别使用﹐就宣告为Private 。
请在看个例子:

'ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'----------------------------------------------------------------------
Class Person
Protected name As String
Private age As Integer
Protected Sub modifyAge(ByVal a As Integer)
age = a
End Sub
Public Sub New(ByVal na As String, ByVal a As Integer)
name = na
age = a
End Sub
Public Sub Show()
Messagebox.Show("Name: " + name + " Age: " + str(age))
End Sub
End Class

Class Teacher
Inherits Person

Public Sub New(ByVal na As String, ByVal a As Integer)
MyBase.New(na, a)
End Sub
Protected Sub modify(ByVal na As String, ByVal a As Integer)
MyBase.name = na
MyBase.modifyAge(a)
End Sub
End Class

Class FullTime_Teacher
Inherits Teacher

Public Sub New(ByVal na As String, ByVal a As Integer)
MyBase.New(na, a)
End Sub
Public Sub modifyValue(ByVal na As String, ByVal a As Integer)
MyBase.modify(na, a)
End Sub
Public Sub modifyData(ByVal na As String, ByVal a As Integer)
MyBase.name = na
MyBase.modifyAge(a)
End Sub
End Class
'-------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
.......
#End Region
Protected Sub Form1_Click( ByVal sender As Object,
ByVal e As System.EventArgs)
Dim p1 As New FullTime_Teacher("David", 25)
p1.Show()
p1.modifyValue("Kent Smith", 45)
p1.Show()
End Sub
End Class

此程序输出:
Name: David Age: 25
Name: Kent Smith Age: 45

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089