复制代码 代码如下:
Light.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Light
/// </summary>
namespace Insus.NET
{
public class Light
{
private int _InputVoltage = 15;
public int InputVoltage
{
get { return _InputVoltage; }
set
{
if (value < 15)
throw new Exception("电压过低。");
else if (value > 15)
throw new Exception("危险!电压过大灯烧坏。");
else
value = 15;
_InputVoltage = value;
}
}
public Light()
{
//
// TODO: Add constructor logic here
//
}
}
}
复制代码 代码如下:
PowerAdapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for PowerAdapter
/// </summary>
namespace Insus.NET
{
public class PowerAdapter : Light
{
Light _Light;
public PowerAdapter(Light light)
{
this._Light = light;
}
public int InputVoltage
{
get
{
return _Light.InputVoltage;
}
set
{
if (value < 15)
throw new Exception("电压过低。");
else if (value > 220)
throw new Exception("危险!电压过大电源适配器烧坏。");
else
value = 15;
_Light.InputVoltage = value;
}
}
}
}
复制代码 代码如下:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function isNumeric(keyCode) {
return ((keyCode >= 48 && keyCode <= 57) || keyCode == 8)
}
</script>
</head>
<body>
<form runat="server">
<table>
<tr>
<td>插座电压</td>
<td colspan="2">
<asp:TextBox runat="server" onkeydown="return isNumeric(event.keyCode);" Text="220"></asp:TextBox></td>
</tr>
<tr>
<td>开关</td>
<td colspan="2">
<asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSwitch_CheckedChanged" /></td>
</tr>
<tr>
<td>灯</td>
<td>
<fieldset>
<legend>Light 1
</legend>
<asp:Image runat="server" ImageUrl="Images/Light_C.gif" /><br />
<asp:Label runat="server" Text=""></asp:Label>
</fieldset>
</td>
<td>
<fieldset>
<legend>Light 2
</legend>
<asp:Image runat="server" ImageUrl="Images/Light_C.gif" /><br />
<asp:Label runat="server" Text=""></asp:Label>
</fieldset>
</td>
</tr>
</table>
</form>
</body>
</html>
复制代码 代码如下:
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
string offLight = "~/Images/Light_C.gif";
string onLight = "~/Images/Light_O.gif";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBoxSwitch_CheckedChanged(object sender, EventArgs e)
{
var cb = (CheckBox)sender;
//插座缺少电压为220伏
int input = Convert.ToInt32(string.IsNullOrEmpty(this.TextBox1.Text.Trim()) ? "220" : this.TextBox1.Text.Trim());
//开关打开
if (cb.Checked)
{
try
{
//实例一个电灯
Light light = new Light();
//插入插座,使用插座电压
light.InputVoltage = input;
//电灯被打开
this.Image1.ImageUrl = onLight;
//显示正常输出电压
this.Label1.Text = light.InputVoltage.ToString();
}
catch (Exception ex)
{
//如果电压不正常,电灯打不开或是被烧坏。
this.Image1.ImageUrl = offLight;
//显示异常信息。
this.Label1.Text = ex.Message;
}
try
{
Light light = new Light();
//使用电源适配器
PowerAdapter pa = new PowerAdapter(light);
pa.InputVoltage = input;
this.Image2.ImageUrl = onLight;
this.Label2.Text = pa.InputVoltage.ToString();
}
catch (Exception ex)
{
this.Image2.ImageUrl = offLight;
this.Label2.Text = ex.Message;
}
this.TextBox1.Enabled = false;
}
//开关关闭
else
{
this.TextBox1.Text = string.Empty;
this.TextBox1.Enabled = true;
this.Image1.ImageUrl = offLight;
this.Image2.ImageUrl = offLight;
}
}
}
新闻热点
疑难解答
图片精选