構造函數是在實例化某個類時提供的一種特殊方法
Public class ShopingCart
private _datecreated As DateTime
private _items As List(Of CartItem)
Public Sub New()
_items=New List(Of CartItem)
_dateCreated=DateTime
End Class
該構造函數的作用是創建另一個對象
Dim Cart As New ShoppingCart()
該語句將創建一個新實例
CartItem類顯示了構造函數的另一個方面
Public Class CartItem
Private _productID As Integer
Private _productName As String
Private _productImageUrl As String
Private _quantity As Integer
Private _price As Double
Private _lineTotal As Double
Public Sub New()
End Sub
Public Sub New(ByVal ProductID As Integer
_productID = ProductID
_productName = ProductName
_productImageUrl = ProductImageUrl
_quantity = Quantity
_price = Price
_lineTotal = Quantity * Price
End Sub
End Class
這裡有兩個構造函數
使用兩個構造函數意味著可以使用兩種方法創建類
Dim item As New CartItem()
或者使用
Dim item As New CartItem(
第一個構造函數創建了一個空的購物車記錄項
[
From:http://tw.wingwit.com/Article/program/net/201311/14619.html