webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dynamicweb3_16.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="Label"></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_16
{
public partial class WebForm1 : System.Web.UI.Page
{
class Animal
{
public virtual string SayHello()
{
return "Animal Say Hello!";
}
}
//多态
class Dog : Animal
{
public override string SayHello()
{
return "Wang Wang!";
}
}
//未使用override,错误了
class Cat : Animal
{
public string SayHello()
{
return "Miao Miao";
}
}
protected void Page_Load(object sender, EventArgs e)
{
Animal what = new Animal();
Label1.Text = what.SayHello();
what = new Dog();
Label1.Text += "<br>" + what.SayHello();
what = new Cat();
Label1.Text += "<br>" + what.SayHello();
}
}
}