webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dynamicweb3_5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace dynamicweb3_5
{
delegate string ToSpeak();
public class Dog1
{
public string ToSpeak()
{
return "汪汪";
}
}
public class Cat1
{
public string ToSpeak()
{
return "喵喵";
}
}
public class Bird1
{
public string ToSpeak()
{
return "咕咕";
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dog1 wangCai = new Dog1();
ToSpeak sp1 = new ToSpeak(wangCai.ToSpeak);
Label1.Text += "狗狗说话:" + sp1() + "<br>";
Cat1 mimi = new Cat1();
sp1 = mimi.ToSpeak;
Label1.Text += "猫咪说话:" + sp1() + "<br>";
Bird1 gugu = new Bird1();
sp1 = gugu.ToSpeak;
Label1.Text += "小鸟说话:" + sp1() + "<br>";
}
}
}