本课学习目标
- 理解类(Class)与对象(Object)的基本关系。
- 使用 Class ... End Class 定义自定义类型。
- 使用 New 建立对象实例。
- 使用 Property 保存对象资料。
- 认识自动实现属性(Auto-implemented Property)。
- 建立构造函数 Sub New。
- 把 Function 和 Sub 写在类中作为对象方法。
- 理解“封装”为什么能让程序结构更清楚。
- 在 Windows Forms 中建立并使用 Student 对象。
- 完成学生资料管理器。
15.1 Class 与 Object 是什么?
类可以理解为“蓝图”,对象则是根据这个蓝图建立出来的实际实例。
例如“学生”是一个概念,而“李明这位学生”是一个具体对象。
Student
定义每个学生应该有什么属性和方法。
student1
根据 Student 类建立出来的一个实际实例。
实例
Object 与 Instance 在初学语境中通常可以理解为同一个概念。
15.2 建立第一个类
在项目中加入一个新的 Class,文件名:
Student.vb
最简单的类:
Public Class Student
End Class
Public Class Student 表示定义一个名为 Student 的类;End Class 表示类定义结束。
15.3 使用 Property 保存对象资料
学生对象需要保存姓名、课程和成绩:
Public Class Student
Public Property Name As String
Public Property Course As String
Public Property Score As Double
End Class
这些都是自动实现属性。
| 属性 | 类型 | 用途 |
|---|---|---|
| Name | String | 学生姓名 |
| Course | String | 课程名称 |
| Score | Double | 学生成绩 |
15.4 使用 New 建立对象
定义类之后,还需要建立实例:
Dim student1 As New Student()
接着设置属性:
student1.Name = "李明"
student1.Course = "Visual Basic 2026"
student1.Score = 88
读取属性:
lblResult.Text =
student1.Name & " - " &
student1.Course & " - " &
student1.Score.ToString("N0")
15.5 同一个类可以建立多个对象
Dim student1 As New Student()
student1.Name = "李明"
student1.Course = "VB.NET"
student1.Score = 88
Dim student2 As New Student()
student2.Name = "王芳"
student2.Course = "C#"
student2.Score = 76
两个对象来自同一个 Student 类,但拥有各自独立的属性值。
15.6 构造函数 Sub New
如果每次建立 Student 后都要逐个设置属性,会有很多重复代码。可以加入构造函数:
Public Class Student
Public Property Name As String
Public Property Course As String
Public Property Score As Double
Public Sub New(
name As String,
course As String,
score As Double)
Me.Name = name
Me.Course = course
Me.Score = score
End Sub
End Class
现在建立对象时直接传入资料:
Dim student1 As New Student(
"李明",
"Visual Basic 2026",
88)
15.7 Me 关键字
构造函数中:
Me.Name = name
左边的 Me.Name 表示“当前 Student 对象的 Name 属性”;右边的 name 是构造函数参数。
Me.Name
=
name
→
对象属性取得参数值
15.8 在类中加入方法
类不仅保存数据,也可以包含行为。例如根据 Score 返回等级:
Public Function GetGrade() As String
Select Case Score
Case Is >= 80
Return "A"
Case Is >= 70
Return "B"
Case Is >= 60
Return "C"
Case Is >= 50
Return "D"
Case Else
Return "F"
End Select
End Function
调用:
Dim grade As String = student1.GetGrade()
注意:这个 Function 不需要 score 参数,因为它直接使用当前对象自己的 Score 属性。
15.9 加入产生摘要的方法
Public Function GetSummary() As String
Return
$"姓名:{Name}" & Environment.NewLine &
$"课程:{Course}" & Environment.NewLine &
$"成绩:{Score:N0}" & Environment.NewLine &
$"等级:{GetGrade()}"
End Function
调用:
lblResult.Text = student1.GetSummary()
对象自己知道怎样把自己的资料组合成摘要,窗体不需要重复处理。
15.10 什么是封装?
封装(Encapsulation)可以先简单理解为:把相关的数据和处理这些数据的方法集中在同一个类中。
Form1 负责所有事情
窗体保存姓名、课程、成绩,还负责等级计算和摘要格式。
Student 管理学生资料
窗体主要负责界面,Student 类负责与学生相关的数据和行为。
15.11 完整的 Student.vb
Public Class Student
Public Property Name As String
Public Property Course As String
Public Property Score As Double
Public Sub New(
name As String,
course As String,
score As Double)
Me.Name = name
Me.Course = course
Me.Score = score
End Sub
Public Function GetGrade() As String
Select Case Score
Case Is >= 80
Return "A"
Case Is >= 70
Return "B"
Case Is >= 60
Return "C"
Case Is >= 50
Return "D"
Case Else
Return "F"
End Select
End Function
Public Function GetSummary() As String
Return
$"姓名:{Name}" & Environment.NewLine &
$"课程:{Course}" & Environment.NewLine &
$"成绩:{Score:N0}" & Environment.NewLine &
$"等级:{GetGrade()}"
End Function
End Class
15.12 实作:学生资料管理器
建立新的 Windows Forms 项目:
StudentProfileManager2026
项目包含两个主要文件:
Form1.vb
Student.vb
课程:Visual Basic 2026
成绩:88
等级:A
在 Form1 加入:
| 控件 | Name | Text / 用途 |
|---|---|---|
| Form | Form1 | Text = 学生资料管理器 |
| TextBox | txtStudentName | 输入学生姓名 |
| TextBox | txtCourse | 输入课程名称 |
| TextBox | txtScore | 输入 0–100 |
| Button | btnCreateStudent | Text = 建立学生对象 |
| Button | btnClear | Text = 清除 |
| Label | lblResult | 显示对象摘要 |
Form1.vb:建立对象
Private Sub btnCreateStudent_Click(
sender As Object,
e As EventArgs) Handles btnCreateStudent.Click
Dim studentName As String =
txtStudentName.Text.Trim()
Dim course As String =
txtCourse.Text.Trim()
Dim score As Double
If String.IsNullOrWhiteSpace(studentName) Then
MessageBox.Show("请输入学生姓名。")
txtStudentName.Focus()
Exit Sub
End If
If String.IsNullOrWhiteSpace(course) Then
MessageBox.Show("请输入课程名称。")
txtCourse.Focus()
Exit Sub
End If
If Not Double.TryParse(txtScore.Text, score) Then
MessageBox.Show("请输入有效成绩。")
txtScore.Focus()
Exit Sub
End If
If score < 0 OrElse score > 100 Then
MessageBox.Show("成绩必须介于 0 与 100。")
txtScore.Focus()
Exit Sub
End If
Dim student As New Student(
studentName,
course,
score)
lblResult.Text = student.GetSummary()
End Sub
清除按钮
Private Sub btnClear_Click(
sender As Object,
e As EventArgs) Handles btnClear.Click
txtStudentName.Clear()
txtCourse.Clear()
txtScore.Clear()
lblResult.Text = ""
txtStudentName.Focus()
End Sub
15.13 程序执行过程
Form1 负责界面和输入验证。
姓名、课程与成绩传给构造函数。
Me.Name、Me.Course 和 Me.Score 保存到对象。
GetGrade 使用自己的 Score 属性。
GetSummary 返回格式化文字给 Form1 显示。
15.14 对象初始化器简介
如果类保留无参数构造方式,也可以使用对象初始化器:
Dim student As New Student With {
.Name = "李明",
.Course = "VB.NET",
.Score = 88
}
这是很方便的语法。不过本课实作使用构造函数,因为它可以要求建立 Student 时必须提供必要资料。
15.15 初学者常见错误
只声明变量,没有 New
Dim student As Student 只是声明变量;要建立实例需要使用 New。
把类名和对象名混在一起
Student 是类型;student1 是某个变量引用的实际对象。
构造函数参数顺序错误
如果参数类型相同,传错顺序可能仍能编译,但资料意义错误。
所有逻辑仍放在 Form1
既然建立 Student 类,就应把真正属于学生的数据和行为逐步移入类中。
15.16 小练习
- 为 Student 加入 StudentId As String 属性。
- 修改构造函数,让 StudentId 也成为必填参数。
- 加入 IsPassing() As Boolean Function,Score ≥ 50 返回 True。
- 在 GetSummary 中加入“状态:及格 / 不及格”。
- 建立 student1 和 student2 两个对象,给不同资料,再分别显示摘要。
- 建立一个 Product 类,包含 ProductName、UnitPrice、StockQuantity,并写一个 CalculateStockValue Function。
本课复习
- Class 与 Object 有什么区别?
- New Student() 的作用是什么?
- Property 用来做什么?
- Sub New 是什么?
- Me.Name = name 中左右两边分别代表什么?
- 为什么同一个 Student 类可以建立多个对象?
- 对象方法与普通 Function 在概念上有什么关系?
- 什么是封装?
- 为什么 Student.GetGrade() 不需要再传入 Score 参数?
- 对象初始化器适合什么情况?