ASP.Net/Language Basics/Class Define — различия между версиями

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

Текущая версия на 11:53, 26 мая 2010

Define a class inside page

<%@ 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()
    _Title = "title"
    _Isbn = 999999999
  End Sub
  Public ReadOnly Property TitleInfo As String
    Get
      Return _Title & " <i>(ISBN: " & _Isbn & ")</i>"
    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()
  Response.Write("<b>New book "MyBook" created.</b>")
  MyBook.Price = "39.99"
  Response.Write("<br/>Title info: " & MyBook.TitleInfo)
  Response.Write("<br/>Price: $" & MyBook.Price & "<br/>")
End Sub
</script>



Define two classes inside a page

<%@ page language="vb" runat="server" Debug="true"%>
<script runat="server">
Public Class Key
  Private _Shape As Integer
  Public Sub New(newshape As Integer)
    _Shape = newshape
  End Sub
  Public ReadOnly Property Shape As Integer
    Get
      Return _Shape
    End Get
  End Property
End Class
Public Class Car
  Private _Color As String
  Private _Gear As Integer
  Private _Ignition As Integer
  Private _EngineRunning As Boolean
  Private Shared _Count = 0
  Public Shared ReadOnly Property Count As Integer
    Get
      Return _Count
    End Get
  End Property
  Public Property Color As String
    Get
      Return _Color
    End Get
    Set(value As String)
      _Color = value
    End Set
  End Property
  Public ReadOnly Property Gear As Integer
    Get
      Return _Gear
    End Get
  End Property
  Public ReadOnly Property IsRunning As String
    Get
      If _EngineRunning Then
        Return "The engine is running."
      Else
        Return "The engine is not running."
      End If
    End Get
  End Property
  Overloads Public Sub ChangeGear(direction As Integer)
    If direction < 0 Then _Gear -= 1
    If direction > 0 Then _Gear += 1
    If _Gear > 5 Then _gear = 5
    If _Gear < -1 Then _gear = -1
  End Sub
  Overloads Public Sub ChangeGear(direction As String)
    If direction = "down" Then ChangeGear(-1)
    If direction = "up" Then ChangeGear(+1)
  End Sub
  Public Sub Ignition(IgnitionKey as Key)
    If IgnitionKey.Shape = _Ignition Then _EngineRunning = True
  End Sub
  Public Sub EngineOff()
    _EngineRunning = False
  End Sub
  Sub New(IgnitionShape as Integer)
    _Color = "cold grey steel"
    _Ignition = IgnitionShape
    _Count += 1
  End Sub
End Class
Sub Page_Load()
  Dim AlexKey As New Key(0987654321)
  Dim RobKey As New Key(1861005040)
  Dim MyKey As New Key(1234567890)
  Dim MyCar As New Car(1234567890)
  Response.Write("<b>New object "MyCar" created.</b>")

  Response.Write("<br/>Color: " & MyCar.Color)
  Response.Write("<br/>Gear: " & MyCar.Gear)
  MyCar.Color = "Black"
  MyCar.ChangeGear(+1)
  Response.Write("<br/><b>Properties updated.</b>")
  Response.Write("<br/>New color: " & MyCar.Color)
  Response.Write("<br/>New gear: " & MyCar.Gear)
  MyCar.ChangeGear("up")
  Response.Write("<br/><b>Shifted "up" one gear.</b>")
  Response.Write("<br/>New gear: " & MyCar.Gear)
  Response.Write("<hr/>Attempting to start MyCar with AlexKey: ")
  MyCar.Ignition(AlexKey)
  Response.Write(MyCar.IsRunning)
  Response.Write("<hr/>Attempting to start MyCar with RobKey: ")
  MyCar.Ignition(RobKey)
  Response.Write(MyCar.IsRunning)
  Response.Write("<hr/>Attempting to start MyCar with MyKey: ")
  MyCar.Ignition(MyKey)
  Response.Write(MyCar.IsRunning)
  Response.Write("<br/>Attempting to stop MyCar: ")
  MyCar.EngineOff()
  Response.Write(MyCar.IsRunning)
End Sub
</script>