Public Class car
Public color As String '名为color的公有属性
Private _speed As Integer '名为_speed的私有属性,私有属性前加_
ReadOnly Property speed() As Integer '只读属性,当读入属性时,get 与 end get 之间的代码就被执行了,返回_speed的值
Get
Return _speed
End Get
End Property
Sub accelerate(ByVal accelerateby As Integer) '创建了一个方法,带有一个参数accelerateby,用来告诉方法把速度加快多少
_speed += accelerateby
End Sub
End Class
2.使用类来查看如何进行工作,打开module1.vb,编写代码如下
Module Module1
Sub main()
Dim mycar As car '创建car类支持的对象
mycar = New car() '创建对象mycar的实例
mycar.color = "red"
Console.WriteLine("车颜色")
Console.WriteLine(mycar.color)
Console.WriteLine("原来车速")
Console.WriteLine(mycar.speed)
mycar.accelerate(5) '调用accelerate方法 Console.WriteLine("加速后的车速") Console.WriteLine(mycar.speed) End SubEnd Module
原文链接:https://www.f2er.com/vb/262189.html