使用JScript.NET製作asp.net網頁基礎教程4

來源:互聯網
上載者:User
在Jscript中定義類通過類聲明, 包含方法和對象和var 聲明。對於類的派生通過下面兩個程式的對比,你講清楚地明白。
    JScript 5.5 Code
// Simple object with no methods
function Car(make, color, year)
{
   this.make = make;
   this.color = color;
   this.year = year;
}
function Car.prototype.GetDescription()
{
   return this.year + " " + this.color + " " + this.make;
}
// Create and use a new Car object
var myCar = new Car("Accord", "Maroon", 1984);
print(myCar.GetDescription());
JScript.NET Code
// Wrap the function inside a class statement.
class Car
{
   var make : String;
   var color : String;
   var year : int;
   function Car(make, color, year)
   {
      this.make = make;
      this.color = color;
      this.year = year;
   }
   function GetDescription()
   {
      return this.year + " " + this.color + " " + this.make;
   }
}
var myCar = new Car("Accord", "Maroon", 1984);
print(myCar.GetDescription());
    Jscript.net還支援定義private和protected property通過GET和SET進行讀寫。
如下例:
class Person
{
   private var m_sName : String;
   private var m_iAge : int;
   function Person(name : String, age : int)
   {
      this.m_sName = name;
      this.m_iAge = age;
   }
   // Name 唯讀
   function get Name() : String
   {
      return this.m_sName;
   }
   // Age 讀寫但是只能用SET
   function get Age() : int
   {
      return this.m_sAge;
   }
   function set Age(newAge : int)
   {
      if ((newAge >= 0) && (newAge <= 110))
         this.m_iAge = newAge;
      else
         throw newAge + " is not a realistic age!";
   }
}
var fred : Person = new Person("Fred", 25);
print(fred.Name);
print(fred.Age);
// 這將產生一個編譯錯誤,name是唯讀。
fred.Name = "Paul";
// 這個將正常執行
fred.Age = 26;
// 這將得到一個 run-time 錯誤, 值太大了
fred.Age = 200;




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.