对象(Object)和类(Class)的关系?
- 对象属于某一类,即对象是某一个类的实例。例如:
Public Class Flight Private _name As String Public Property Name As String Get Return _name End Get Set(value As String) _name = value End Set End Property Public Sub New() End Sub Public Sub New(ByVal name As String) _name = name End Sub End Class Sub Main() Dim f1 = New Flight("MH370")
在上面的示例代码中Flight是类,表示“航班”;而f1是对象,用来表示具体的某一个航班,名叫“MH370”
2. 类是具有相同属性和行为的某一类对象的定义/描述
例如:我们可以将汽车、列车、飞机、轮船等这些具有运输能力,包含发动机的一系列对象定义一个类,名叫:Vehicle
Public Class Vehicle Private _speed As Double Public Property Speed As Double Get End Get Set(value As Double) End Set End Property '剩余燃料 Public Property ResidualFuel As Double Get End Get Set(value As Double) End Set End Property '启动 Public Overridable Sub Start() Call EngineStart() End Sub '紧急停止 Public Sub StopImmediate() End Sub '引擎启动 Public Sub EngineStart() Console.WriteLine("引擎启动...") End Sub Public Sub Whistle() End Sub '加/减速 ''' <param name="increment">增量</param> Public Sub Accelerate(ByVal increment As Double) End Sub End Class