接口:猫、老鼠、主人面试题的接口实现

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  AspNetTest.Common
{
    
/// <summary>
    
/// Define_Delegate_CatMouse_Interface 的摘要说明。
    
/// </summary>

    public class Define_Delegate_CatMouse_Interface : System.Web.UI.Page
        
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            Cat1 cat 
= new Cat1();
            Mouse1 mouse1 
= new Mouse1(cat, "老鼠1");
            Mouse1 mouse2 
= new Mouse1(cat, "老鼠2");
            Master1 master 
= new Master1(cat);
            cat.Shout();
            
// 在此处放置用户代码以初始化页面
        }

    
        
public interface IAffectSubject
        
{    
            
void Response();
        }

        
public interface IOperateAffectSubject
        
{
            
void AddAffectSubject(IAffectSubject affectSubject);
        }

        
public class Master1 : IAffectSubject
        
{
            
public Master1(IOperateAffectSubject iOperateAffectSubject)
            
{
                iOperateAffectSubject.AddAffectSubject(
this);
            }

            
public void Response()
            
{
                HttpContext.Current.Response.Write(
"主人醒了!<br>");                                    
            }

        }

        
public class Mouse1 : IAffectSubject
        
{
            
public Mouse1(IOperateAffectSubject iOperateAffectSubject, string name)
            
{
                iOperateAffectSubject.AddAffectSubject(
this);
                
this.name = name;
            }

            
private string name;
            
public void Response()
            
{
                HttpContext.Current.Response.Write(name 
+ "跑了!<br>");                    
            }

        }

        
public class Cat1 : IOperateAffectSubject
        
{
            
public Cat1()
            
{
                affectSubjects 
= new ArrayList();
            }

            
private ArrayList affectSubjects;
            
public void AddAffectSubject(IAffectSubject affectSubject)
            
{
                affectSubjects.Add(affectSubject);
            }

            
public void Shout()
            
{
                HttpContext.Current.Response.Write(
"猫大叫!<br>");
                
foreach(IAffectSubject subject in affectSubjects)
                
{
                    subject.Response();
                }

            }

        }

        
        
Web 窗体设计器生成的代码
    }

}

你可能感兴趣的:(面试题)