-
vb.net教程之类的属性
类的属性(相关vb.net教程)
1、属性的概念
在系统类中提供了很多属性,如textbox中有text属性。类的属性是一个可以设置值(赋值),也能取出值(引用)的成员。
初看起来,属性与类中的变量十分相似,但实际上是不同的。在对类的变量设置值时是没有检测功能的,例如设置月份为13月显然是不正确的。
显然需要一种机制,它可以在为对象的成员变量设置值时作检查,这就是属性。属性实际上是一种访问成员变量的特殊函数。同时,为了保护类的成员变量不被随意更改,一般被设置成为private,这样在类的外面通过对象就不能访问,只能通过一种特殊函数—属性去访问它。
2、属性的定义
Public property 属性名称
Get ‘引用属性
…
End get
Set(byval value as 类型) ‘设置属性
…
End set
End property
例.编写mydate中的year—年 、month—月、 day—日属性,以完成对成员变量m_year 、m_month、 m_day的读写并测试。
程序见,类的属性应用,程序为:
Public Class mydate
Private m_year, m_month, m_day As Integer
Public Sub New()
setsystemdate()
End Sub
Public Sub New(ByVal y As Integer, ByVal m As Integer, ByVal d As Integer)
m_year = y : m_month = m : m_day = d
End Sub
Public Sub setsystemdate()
m_year = Microsoft.VisualBasic.Year(Now())
m_month = Microsoft.VisualBasic.Month(Now)
m_day = Microsoft.VisualBasic.Day(Now)
End Sub
Public Sub show()
MsgBox(Str(m_year) + "-" + Str(m_month) + "-" + Str(m_day), MsgBoxStyle.OkOnly, "日期")
End Sub
Public Property month() As Integer
Get
month = m_month
End Get
Set(ByVal m As Integer)
m_month = m
End Set
End Property
Public Property year() As Integer
Get
year = m_year
End Get
Set(ByVal y As Integer)
m_year = y
End Set
End Property
Public Property day() As Integer
Get
day = m_day
End Get
Set(ByVal d As Integer)
m_day = d
End Set
End Property
End Class
测试类为:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As New mydate
a.year = 1996
a.month = 10
a.day = 6
a.show()
End Sub
End Class
说明:
上面程序对年月日的有效性还不能进行正确性检查,改进的方法是课本P165-P166的程序,从略。
1、属性的概念
在系统类中提供了很多属性,如textbox中有text属性。类的属性是一个可以设置值(赋值),也能取出值(引用)的成员。
初看起来,属性与类中的变量十分相似,但实际上是不同的。在对类的变量设置值时是没有检测功能的,例如设置月份为13月显然是不正确的。
显然需要一种机制,它可以在为对象的成员变量设置值时作检查,这就是属性。属性实际上是一种访问成员变量的特殊函数。同时,为了保护类的成员变量不被随意更改,一般被设置成为private,这样在类的外面通过对象就不能访问,只能通过一种特殊函数—属性去访问它。
2、属性的定义
Public property 属性名称
Get ‘引用属性
…
End get
Set(byval value as 类型) ‘设置属性
…
End set
End property
例.编写mydate中的year—年 、month—月、 day—日属性,以完成对成员变量m_year 、m_month、 m_day的读写并测试。
程序见,类的属性应用,程序为:
Public Class mydate
Private m_year, m_month, m_day As Integer
Public Sub New()
setsystemdate()
End Sub
Public Sub New(ByVal y As Integer, ByVal m As Integer, ByVal d As Integer)
m_year = y : m_month = m : m_day = d
End Sub
Public Sub setsystemdate()
m_year = Microsoft.VisualBasic.Year(Now())
m_month = Microsoft.VisualBasic.Month(Now)
m_day = Microsoft.VisualBasic.Day(Now)
End Sub
Public Sub show()
MsgBox(Str(m_year) + "-" + Str(m_month) + "-" + Str(m_day), MsgBoxStyle.OkOnly, "日期")
End Sub
Public Property month() As Integer
Get
month = m_month
End Get
Set(ByVal m As Integer)
m_month = m
End Set
End Property
Public Property year() As Integer
Get
year = m_year
End Get
Set(ByVal y As Integer)
m_year = y
End Set
End Property
Public Property day() As Integer
Get
day = m_day
End Get
Set(ByVal d As Integer)
m_day = d
End Set
End Property
End Class
测试类为:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As New mydate
a.year = 1996
a.month = 10
a.day = 6
a.show()
End Sub
End Class
说明:
上面程序对年月日的有效性还不能进行正确性检查,改进的方法是课本P165-P166的程序,从略。
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比