您的位置:首页 > 编程语言 > ASP

使用JScript.NET创建asp.net页面(四)

2008-05-01 06:16 134 查看
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;

<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: