Asp.net中使用对象的实例
2005-03-21 16:49
851 查看
<%@ Page Language="C#" runat="server" Debug="true" %>
<script runat="server">
public class Key
{
private int shape;
public Key(int newshape)
{
shape = newshape;
}
public int Shape
{
get
{
return shape;
}
}
}
public class Car
{
private string color;
private int gear;
private int ignition;
private bool engineRunning;
private static int count = 0;
public Car(int IgnitionShape)
{
color = "Cold gray steel";
ignition = IgnitionShape;
count += 1;
}
public static int Count
{
get
{
return count;
}
}
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
public int Gear
{
get
{
return gear;
}
}
public string IsRunning
{
get
{
if (engineRunning)
{
return "The engine is running.";
}
else
{
return "The engine is not running.";
}
}
}
public void ChangeGear(int direction)
{
if (direction < 0) gear -= 1;
if (direction > 0) gear += 1;
if (gear > 5) gear = 5;
if (gear < -1) gear = -1;
}
public void ChangeGear(string direction)
{
if (direction == "down")
{
ChangeGear(-1);
}
if (direction == "up")
{
ChangeGear(+1);
}
}
public void Ignition(Key IgnitionKey)
{
if (IgnitionKey.Shape == ignition) engineRunning = true;
}
public void EngineOff()
{
engineRunning = false;
}
}
public void Page_Load()
{
Key AlexKey = new Key(0987654321);
Key RobKey = new Key(1861005040);
Key MyKey = new Key(1234567890);
Car MyCar = new Car(1234567890);
Response.Write("<b>New object 'MyCar' created.</b>");
Response.Write("<br/>Color: " + MyCar.Color);
Response.Write("<br/>Gear: " + MyCar.Gear);
MyCar.Color = "Black";
MyCar.ChangeGear(+1);
Response.Write("<br/><b>Properties updated.</b>");
Response.Write("<br/>New color: " + MyCar.Color);
Response.Write("<br/>New gear: " + MyCar.Gear);
MyCar.ChangeGear("up");
Response.Write("<br/><b>Shifted 'up' one gear.</b>");
Response.Write("<br/>New gear: " + MyCar.Gear);
Response.Write("<hr/>Attempting to start MyCar with AlexKey: ");
MyCar.Ignition(AlexKey);
Response.Write(MyCar.IsRunning);
Response.Write("<hr/>Attempting to start MyCar with RobKey: ");
MyCar.Ignition(RobKey);
Response.Write(MyCar.IsRunning);
Response.Write("<hr/>Attempting to start MyCar with MyKey: ");
MyCar.Ignition(MyKey);
Response.Write(MyCar.IsRunning);
Response.Write("<br/>Attempting to stop MyCar: ");
MyCar.EngineOff();
Response.Write(MyCar.IsRunning);
}
</script>
<html>
<head>
</head>
<body>
</body>
</html>
<script runat="server">
public class Key
{
private int shape;
public Key(int newshape)
{
shape = newshape;
}
public int Shape
{
get
{
return shape;
}
}
}
public class Car
{
private string color;
private int gear;
private int ignition;
private bool engineRunning;
private static int count = 0;
public Car(int IgnitionShape)
{
color = "Cold gray steel";
ignition = IgnitionShape;
count += 1;
}
public static int Count
{
get
{
return count;
}
}
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
public int Gear
{
get
{
return gear;
}
}
public string IsRunning
{
get
{
if (engineRunning)
{
return "The engine is running.";
}
else
{
return "The engine is not running.";
}
}
}
public void ChangeGear(int direction)
{
if (direction < 0) gear -= 1;
if (direction > 0) gear += 1;
if (gear > 5) gear = 5;
if (gear < -1) gear = -1;
}
public void ChangeGear(string direction)
{
if (direction == "down")
{
ChangeGear(-1);
}
if (direction == "up")
{
ChangeGear(+1);
}
}
public void Ignition(Key IgnitionKey)
{
if (IgnitionKey.Shape == ignition) engineRunning = true;
}
public void EngineOff()
{
engineRunning = false;
}
}
public void Page_Load()
{
Key AlexKey = new Key(0987654321);
Key RobKey = new Key(1861005040);
Key MyKey = new Key(1234567890);
Car MyCar = new Car(1234567890);
Response.Write("<b>New object 'MyCar' created.</b>");
Response.Write("<br/>Color: " + MyCar.Color);
Response.Write("<br/>Gear: " + MyCar.Gear);
MyCar.Color = "Black";
MyCar.ChangeGear(+1);
Response.Write("<br/><b>Properties updated.</b>");
Response.Write("<br/>New color: " + MyCar.Color);
Response.Write("<br/>New gear: " + MyCar.Gear);
MyCar.ChangeGear("up");
Response.Write("<br/><b>Shifted 'up' one gear.</b>");
Response.Write("<br/>New gear: " + MyCar.Gear);
Response.Write("<hr/>Attempting to start MyCar with AlexKey: ");
MyCar.Ignition(AlexKey);
Response.Write(MyCar.IsRunning);
Response.Write("<hr/>Attempting to start MyCar with RobKey: ");
MyCar.Ignition(RobKey);
Response.Write(MyCar.IsRunning);
Response.Write("<hr/>Attempting to start MyCar with MyKey: ");
MyCar.Ignition(MyKey);
Response.Write(MyCar.IsRunning);
Response.Write("<br/>Attempting to stop MyCar: ");
MyCar.EngineOff();
Response.Write(MyCar.IsRunning);
}
</script>
<html>
<head>
</head>
<body>
</body>
</html>
相关文章推荐
- Asp.net中对象使用的实例
- ASP.NET在.ashx文件中使用session出异常,未将对象引用到对象的实例。
- ASP.NET在.ashx文件中使用session出异常,未将对象引用到对象的实例
- ASP.NET 广告控件AdRotator的使用方法与实例
- ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象
- ASP.NET使用Memcached高缓存实例
- asp.net报错:“System.NullReferenceException: 未将对象引用设置到对象的实例”
- asp.net f发布网站 出现未将对象应用到实例
- asp.net内置对象 Response对象使用介绍
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 对象序列化(七):应用实例->asp.net ViewState
- ASP.NET 对象的使用
- ASP.NET使用Memcached高缓存实例
- ASP.NET中application对象的使用
- Asp.net内置对象之Cookies(简介/属性方法/基本操作及实例)
- Asp.net core WebApi 使用Swagger生成帮助页实例
- 使用生活实例理解Asp.net运行时
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- ASP.NET中水晶报表的使用实例
- 使用ASP.NET AJAX调用后台WebService优化网站性能实例