-
vb.net教程之类的构造方法(函数)
类的构造方法(函数)(课本5.2)(相关vb.net教程)
1、构造方法是名称是new的过程,前面要以public说明。
2、同时可以定义多个同名的构造方法,只要它的参数个数或类型不同,此时称为重载。以增加构造方法的适用范围。
3、构造方法只在创建对象时由系统调用,不能在程序中人为调用。
修改上面“我的日期类”类,得到程序:类的构造方法,程序为:
Public Class mydate
Public m_year, m_month, m_day As Integer
Public Sub New()
m_year = Microsoft.VisualBasic.Year(Now)
m_month = Microsoft.VisualBasic.Month(Now)
m_day = Microsoft.VisualBasic.Day(Now)
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 show()
MsgBox(Str(m_year) + "-" + Str(m_month) + "-" + Str(m_day), MsgBoxStyle.OkOnly, "日期")
End Sub
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
Dim b As New mydate(2008, 5, 1)
a.show()
b.show()
End Sub
End Class
应用实例:用定义类的方法编程解决,任输入矩形的两边,求其周长与面积。
见,类的应用矩形问题,程序为:
Public Class juxing
Public x, y As Integer
Public Function p()
p = 2 * (x + y)
End Function
Public Function area()
area = x * y
End Function
End Class
Public Class Form1
Dim a As New juxing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pp, ss As Double
pp = a.p
ss = a.area
Console.WriteLine("周长为:" + Str(pp))
Console.WriteLine("面积为:" + Str(ss))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
a.x = Val(TextBox1.Text)
a.y = Val(TextBox2.Text)
End Sub
End Class
1、构造方法是名称是new的过程,前面要以public说明。
2、同时可以定义多个同名的构造方法,只要它的参数个数或类型不同,此时称为重载。以增加构造方法的适用范围。
3、构造方法只在创建对象时由系统调用,不能在程序中人为调用。
修改上面“我的日期类”类,得到程序:类的构造方法,程序为:
Public Class mydate
Public m_year, m_month, m_day As Integer
Public Sub New()
m_year = Microsoft.VisualBasic.Year(Now)
m_month = Microsoft.VisualBasic.Month(Now)
m_day = Microsoft.VisualBasic.Day(Now)
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 show()
MsgBox(Str(m_year) + "-" + Str(m_month) + "-" + Str(m_day), MsgBoxStyle.OkOnly, "日期")
End Sub
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
Dim b As New mydate(2008, 5, 1)
a.show()
b.show()
End Sub
End Class
应用实例:用定义类的方法编程解决,任输入矩形的两边,求其周长与面积。
见,类的应用矩形问题,程序为:
Public Class juxing
Public x, y As Integer
Public Function p()
p = 2 * (x + y)
End Function
Public Function area()
area = x * y
End Function
End Class
Public Class Form1
Dim a As New juxing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pp, ss As Double
pp = a.p
ss = a.area
Console.WriteLine("周长为:" + Str(pp))
Console.WriteLine("面积为:" + Str(ss))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
a.x = Val(TextBox1.Text)
a.y = Val(TextBox2.Text)
End Sub
End Class
栏目列表
最新更新
求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() 对比