Using JScript.NET to make asp.net Web page Basics Tutorial 4
Source: Internet
Author: User
A class is defined in JScript by a class declaration that contains methods and objects and VAR declarations. The derivation of the class is made clear by the comparison of the following two programs.
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 also supports the definition of private and protected property to read and write through get and set.
The following example:
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 Read Only
function Get Name (): String
{
return this.m_sname;
}
Age reads and writes but can only use 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);
This will result in a compilation error, and name is read-only.
Fred. Name = "Paul";
This will be performed normally.
Fred. Age = 26;
This will get a run-time error, the value is too large
Fred. Age = 200;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service