ASP.Net/Language Basics/Class Constrctuctor

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Define and use constructor

   <source lang="csharp">

<%@ page language="vb" runat="server" %> <script runat="server"> Public Class Book

 Private _Title As String
 Private _Isbn As Integer
 Private _Price As Decimal
 Public Sub New(newTitle As String, newIsbn As Integer)
   _Title = newTitle
   _Isbn = newIsbn
 End Sub
 Public ReadOnly Property TitleInfo As String
   Get
     Return _Title & " (ISBN: " & _Isbn & ")"
   End Get
 End Property
 Public ReadOnly Property Title As String
   Get
     Return _Title
   End Get
 End Property
 Public ReadOnly Property Isbn As Integer
   Get
     Return _Isbn
   End Get
 End Property
 Public Property Price As Decimal
   Get
     Return _Price
   End Get
   Set(value As Decimal)
     _Price = value
   End Set
 End Property

End Class Sub Page_Load()

 Dim MyBook As New Book("title 0", 99999999999)
 Response.Write("New book "MyBook" created.")
 MyBook.Price = "39.99"
 Response.Write("
Title info: " & MyBook.TitleInfo) Response.Write("
Price: $" & MyBook.Price & "
") Dim AnotherBook As New Book("title", 99999999999) Response.Write("New book "AnotherBook" created.") AnotherBook.Price = "59.99" Response.Write("
Title info: " & AnotherBook.TitleInfo) Response.Write("
Price: $" & AnotherBook.Price & "
")

End Sub </script>

      </source>