首页 > 编程 > .NET > 正文

使用JScript.NET制作asp.net网页基础教程4

2024-07-10 12:54:52
字体:
来源:转载
供稿:网友
在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;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表