print(fred.Name);
print(fred.Age);
// 这将产生一个编译错误,name是只读的。
fred.Name = "Paul";
// 这个将正常执行
fred.Age = 26;
// 这将得到一个 run-time 错误, 值太大了
fred.Age = 200;
Jscript.net可以用JScript 或任意NET 框架语言(如 C #,VB7.0) 通过增加extends主题词在类声明以后来继承和扩展现有类。这能力允许Jscript.net非常容易地利用 NET 平台的丰厚资源。为了说明这些,给出一个程序。这个程序扩展了NET 框架的ServiceBase 类。
// 导入需要的.net命名空间
import System;
import System.ServiceProcess;
import System.Diagnostics;
import System.Timers;
class SimpleService extends ServiceBase
{
private var timer : Timer;
function SimpleService()
{
CanPauseAndContinue = true;
ServiceName = "JScript Service";
timer = new Timer();
timer.Interval = 1000;
timer.AddOnTimer(OnTimer);
}
protected override function OnStart(args : String[])
{
EventLog.WriteEntry("JScript Service started");
timer.Enabled = true;
}
protected override function OnStop()
{
EventLog.WriteEntry("JScript Service stopped");
timer.Enabled = false;
}
protected override function OnPause()
{
EventLog.WriteEntry("JScript Service paused");
timer.Enabled = false;
}
protected override function OnContinue()
{
EventLog.WriteEntry("JScript Service continued");
timer.Enabled = true;
}
function OnTimer(source : Object, e : EventArgs)
{
EventLog.WriteEntry("Hello World from JScript!");
}
}
ServiceBase.Run(new SimpleService());
如何在asp 中使用Jscript.net这才是我们关键的问题。我们将通过一个例子来说明这个问题。
访问sqlserver数据库
第一个aps 例子是使用Jscript.net和.net的数据访问类来访问sqlserver数据库,
这里我还将使用大家熟悉的<% %>格式来编写,访问pubs中的authors表,我知道这很简单但是它可以体现一些新的特性。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<%@ language="JScript" %>
<link rel="STYLESHEET" type="text/css" href="style.css">
<%
// 设置数据库连接
var myConnection:SQLConnection = new SQLConnection("server=scripting;uid=sa;pwd=;database=pubs");
// 执行查询
var myCommand:SQLDataSetCommand = new SQLDataSetCommand("select * from Authors", myConnection);
// 声明变量
var ds:DataSet = new DataSet();
var myTable:DataTable
var myColumns:ColumnsCollection
var myCol:DataColumn
var myRows:RowsCollection
var myRow:DataRow
// 通过FillDataSet方法获取数据
myCommand.FillDataSet(ds, "Authors");
myTable = ds.Tables[0]
%>
<h1>
<%=ds.Tables[0].TableName%>
</h1>
<br>
<TABLE>
<THEAD>
<TR>
<%
//在表格的最上面输出字段名
myColumns = myTable.Columns
for (myCol in myColumns)
{
%>
<TH class="spec">
<%=myCol.ColumnName%>
</TH>
<%
}
%>
</TR>
</THEAD>
<%
// 输出所有的纪录
myRows = myTable.Rows
for (myRow in myRows)
{
%>
<TR>
<%
for(var i:int=0;i<myColumns.Count;i )
{
%>
<TD class="spec">
<%=myRow[i]%>
</TD>
<%
}
%>
</TR>
<%
}
%>
</TABLE>
例子2
<%@ WebService Language="JScript" class="Weather"%>
import System
import System.Web.Services
class Weather {
WebMethodAttribute function getConditions(strCity : String) : String
{
var now = new Date();
switch (strCity.toUpperCase())
{
case "LONDON":
if (now.getMonth() <= 7||now.getMonth() >=9)
{
return "overcast"
}
if
{
return "partly overcast"
}
break;
case "SEATTLE":
if (now.getMonth() == 7 && now.getDay()==4)
{
return "torrential rain"
}
else
{
return "rain"
}
break;
case "LA":
return "smoggy"
break;
case "PHOENIX":
return "damn hot"
break;
default:
return "partly cloudy with a chance of showers"
}
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



