下面的程序定义了一个产品类Product,有以下成员: (1)数据成员:Name、Price和Quantity,分别表示产品名称、单价和剩余数量。 (2)构造函数:New (3)方法:Buy和Display。Buy方法根据顾客资金和产品剩余数量决定购买情况,Display方法输出产品的剩余情况。 类Form1中的Button1_Click事件过程是用于测试所定义的类。程序运行结果如图10.13所示。 Public Class Form1 Inherits System.Windows.Forms.Form SubButton1_Click( ...... ) Handles Button1.Click Dim p As NewProduct("电视机", 2000, 15) p.Buy(7000) p.Display() p.Buy(4500) p.Display() End Sub End Class Class Product Private NameAs String PrivatePrice As Integer PrivateQuantity As Integer Public Sub (ByVal n As String, ByVal p As Integer, ByVal q As Integer) Name = n Price = p End Sub Public SubBuy(ByVal Money As Integer) Dim n, r AsInteger If n >Quantity Then Debug.WriteLine("数量不够!") Else Quantity = Quantity - n r = MoneyMod Price Debug.WriteLine("产品:" &Name & " 单价:" &Price & "元 顾客" _ & Money& "元,买了" & n & "台,剩余" &r & "元") End If End Sub Public SubDisplay() Debug.WriteLine("产品:" &Name & " 单价:" &Price & "元 剩余" &Quantity & "台") End Sub End Class