ASP.Net/Language Basics/Class Definition

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

Constructor with parameter (C#)

   <source lang="csharp">

<%@ page Language="c#" runat="server" %> <script runat="server"> public class Book {

 private string title;
 private int isbn;
 private decimal price;
 public Book(string newTitle, int newIsbn)
 {
   title = newTitle;
   isbn = newIsbn;
 }
 public string TitleInfo
 {
    get 
    {
       return title + " [ISBN: " + isbn + "]";
    }
 }
 public string Title
 {
   get 
   {
      return title;
   }
 }
 public int Isbn
 {
   get 
   {
     return isbn;
   }
 }
 public decimal Price
 {
   get 
   {
     return price;
   }
   set 
   {
     price = value;
   }
 }

}

 void Page_Load()
 {
   Book MyBook = new Book("Beginning ASP.NET 1.0 with C#", 1861007345);
   Response.Write("new book "MyBook" created.");
   MyBook.Price = 39.99m;
   Response.Write("
Title info: " + MyBook.TitleInfo); Response.Write("
Price: $" + MyBook.Price + "
"); Book AnotherBook = new Book("Professional ASP.NET 1.0 SE", 1861007035); Response.Write("new book "AnotherBook" created."); AnotherBook.Price = 59.99m; Response.Write("
Title info: " + AnotherBook.TitleInfo); Response.Write("
Price: $" + AnotherBook.Price + "
"); }

</script>

      </source>
   
  


Define a class and display its value (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

 public class Person
 {
   private string  _Name;
   private int _Age;
   private string  _EyeColor;
   public Person() {
   }
   public string  Name {
     get {
       return _Name;
     }
     set {
       _Name = value;
     }
   }
   public int Age {
     get {
       return _Age;
     }
     set {
       _Age = value;
     }
   }
   public string EyeColor {
     get {
       return _EyeColor;
     }
     set {
       _EyeColor = value;
     }
   }
 }
 void Page_Load(object Sender, EventArgs E) {
   Person p = new Person();
   p.Name = "Joe";
   p.Age = 25;
   p.EyeColor = "Blue";
   Name.Text = p.Name;
   Age.Text = p.Age.ToString();
   EyeColor.Text = p.EyeColor;
 }

</script> <html> <head> </head> <body>

   <form runat="server">
     Name: <asp:Label runat="server" id="Name" />
Age: <asp:Label runat="server" id="Age" />
Eye Color: <asp:Label runat="server" id="EyeColor" /> </form>

</body> </html>

      </source>
   
  


Define and use class: property (C#)

   <source lang="csharp">

<%@ page Language="c#" runat="server" %> <script runat="server"> public class Book {

 private string title;
 private int isbn;
 private decimal price;
 public Book()
 {
   title = "Book";
   isbn = 099999999999;
 }
 public string TitleInfo
 {
    get 
    {
       return title + " [ISBN: " + isbn + "]";
    }
 }
 public string Title
 {
   get 
   {
      return title;
   }
 }
 public int Isbn
 {
   get 
   {
     return isbn;
   }
 }
 public decimal Price
 {
   get 
   {
     return price;
   }
   set 
   {
     price = value;
   }
 }

}

 void Page_Load()
 {
   Book MyBook = new Book();
   Response.Write("new book "MyBook" created.");
   MyBook.Price = 39.99m;
   Response.Write("
Title info: " + MyBook.TitleInfo); Response.Write("
Price: $" + MyBook.Price + "
"); }

</script>

      </source>
   
  


Define two classes (C#)

   <source lang="csharp">

<%@ page language="C#" runat="server" Debug="true"%> <script runat="server">

 public class Key
 {
   private int shape;
   public Key(int newshape)
   {
     shape = newshape;
   }
   public int Shape
   {
     get
     {
       return shape;
     }
   }
 }
   
   public class Car
 {
   private string color;
   private int gear;
   private int ignition;
   private bool engineRunning;
   private static int count = 0;
   public Car(int IgnitionShape)
   {  
     color = "Cold gray steel";
     ignition = IgnitionShape;
     count += 1;
   }
   public static int Count
   {
     get
     {
       return count;
     }
   }
   public string Color
   {
     get
     {
       return color;
     }
     set
     {
       color = value;
     }
   }
   public int Gear
   {
     get
     {
       return gear;
     }
   }
   public string IsRunning  
   {
     get
     {
       if (engineRunning)
       {
         return "The engine is running.";
       }
       else
       {
         return "The engine is not running.";
       }
     }
   }
   public void ChangeGear(int direction)
   {
     if (direction < 0) gear -= 1;
     if (direction > 0) gear += 1;
     if (gear > 5) gear = 5;
     if (gear < -1) gear = -1;
   }
   
   public void ChangeGear(string direction)
   {
     
     if (direction == "down")
     {
       ChangeGear(-1);
     }
     if (direction == "up")
     {
       ChangeGear(+1);
     }
     
   }
   public void Ignition(Key IgnitionKey)
   {
     if (IgnitionKey.Shape == ignition) engineRunning = true;
   }
   public void EngineOff()
   {
     engineRunning = false;
   }
 }
   
   public void Page_Load()
 {
   Key AlexKey = new Key(0987654321);
   Key RobKey = new Key(1861005040);
   Key MyKey = new Key(1234567890);
   Car MyCar = new Car(1234567890);
   Response.Write("New object "MyCar" created.");
   Response.Write("
Color: " + MyCar.Color); Response.Write("
Gear: " + MyCar.Gear); MyCar.Color = "Black"; MyCar.ChangeGear(+1); Response.Write("
Properties updated."); Response.Write("
New color: " + MyCar.Color); Response.Write("
New gear: " + MyCar.Gear); MyCar.ChangeGear("up"); Response.Write("
Shifted "up" one gear."); Response.Write("
New gear: " + MyCar.Gear);
Response.Write("
Attempting to start MyCar with AlexKey: ");
   MyCar.Ignition(AlexKey);
   Response.Write(MyCar.IsRunning);
Response.Write("
Attempting to start MyCar with RobKey: ");
   MyCar.Ignition(RobKey);
   Response.Write(MyCar.IsRunning);
Response.Write("
Attempting to start MyCar with MyKey: ");
   MyCar.Ignition(MyKey);
   Response.Write(MyCar.IsRunning);
   Response.Write("
Attempting to stop MyCar: "); MyCar.EngineOff(); Response.Write(MyCar.IsRunning); }

</script>

      </source>