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

把 hei变量值存入对象 a之资料成员──height中。

 

此刻﹐对象 a对讯息之处理完成了﹐其内部资料改变了﹐亦即对象 a之内部状态(Internal State)改变了﹔这是对象的行为之一。上述您已经会加入一个程序了﹐依同样方法﹐继续加入其它程序﹐让对象的兴为更多采多姿。例如﹕

'ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-----------------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
End Sub
Public Function inquireHeight() As Single
inquireHeight = height
End Function
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 a As Tree = New Tree()
Dim h As Single
a.input(2.1)
h = a.inquireHeight()
Messagebox.Show("height = " + str(h) + "公尺", "HI!")
End Sub
End Class

此程序输出如下﹕height = 2.1公尺
Tree类别有2个程序成员──input() 和inquireHeight()。类别之程序成员必须与其对象配合使用。格式为﹕

亦即﹐必须以讯息之形式出现。例如﹕

如果程序成员不与对象相配合时﹐计算机会如何处理呢﹖例如﹕

'ex04.bas
'Some Error Here !
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'--------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
End Sub
Public Function inquireHeight() As Single
inquireHeight = height
End Function
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 a As Tree = New Tree()
Dim h As Single
a.input(2.1)
h = inquireHeight()
Messagebox.Show("height = " + str(h) + "公尺", "HI!")
End Sub
End Class

当计算机看到Form1_Click()内之指令──
h = inquireHeight( )

(编辑: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