• ASP.NET学习笔记3


    2016.4.12

    1、
    IEnumerable接口:
    GetEnumerator()方法,用于返回一个循环访问集合的枚举数
    IEnumerator()接口:
    Object Current{get;}属性,获取集合中的当前元素
    bool MoveNext()方法,访问集合的下一个元素
    void Reset()方法,设置枚举数为其初始位置
     1 public IEnumerator GetEnumerator()
     2         {
     3             return this;
     4             //return this as IEnumerator;
     5         }
     6         //IEnumerator接口的Current属性
     7         public object Current
     8         {
     9             get
    10             {
    11                 switch (flag)
    12                 {
    13                     case 0:
    14                         return "奥迪";
    15                     case 1:
    16                         return "皇冠";
    17                     case 2:
    18                         return "宝马";
    19                     case 3:
    20                         return "奔驰";
    21                     default:
    22                         return "OH,This is Error!";
    23                 }
    24             }
    25         }
    26         //IEnumerator接口的MoveNext方法
    27         public bool MoveNext()
    28         {
    29             flag++;
    30             if (flag == 4)
    31                 return false;
    32             return true;
    33         }
    34         //IEnumerator接口的Reset方法
    35         public void Reset()
    36         {
    37             flag = -1;
    38         }
    IDisposable接口:
    Dispose()方法,执行与释放或重置非托管资源相关应用程序定义的任务。  通过using语句可以释放对象。
    IComparable接口:
    int CompareTo(Object obj)方法,用于返回与obj的大小关系
     1 //实现IComparable.CompareTo方法
     2 //注意无修饰符
     3 int IComparable.CompareTo(object obj)
     4             {
     5                 Student s = (Student)obj;
     6                 if (this.sid > s.sid)
     7                     return 1;
     8                 if (this.sid < s.sid)
     9                     return -1;
    10                 else
    11                     return 0;
    12             }
    2、委托
    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。
     1  public delegate string GreetingDelegate(string name);//定义委托
     2     public partial class Default : System.Web.UI.Page
     3     {
     4         //接受一个GreetingDelegate类型的Make方法作为参数
     5         public string GreetPeople(string name, GreetingDelegate Make)
     6         {
     7             return Make(name);
     8         }
     9         public static string EnglishGreeting(string name)
    10         {
    11             return "Morning," + name;
    12         }
    13         public static string ChineseGreeting(string name)
    14         {
    15             return "早上好," + name;
    16         }
    17         protected void Page_Load(object sender, EventArgs e)
    18         {
    19             Response.Write(GreetPeople("Jimmy", EnglishGreeting)+"<br/>");
    20             Response.Write(GreetPeople("李四", ChineseGreeting)+"<br/>");
    21 
    22             GreetingDelegate gd1, gd2;
    23             gd1 = EnglishGreeting;
    24             gd2 = ChineseGreeting;
    25             Response.Write(GreetPeople("Ondina", gd1) + "<br/>");
    26             Response.Write(GreetPeople("星夜", gd2) + "<br/>");
    27 
    28             GreetingDelegate gd3;
    29             gd3 = EnglishGreeting;//先给委托变量赋值
    30             gd3 += ChineseGreeting;// 给此委托变量再绑定一个方法
    31             // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法,但只能接收到一个返回值
    32             Response.Write(GreetPeople("Fullbuster", gd3) + "<br/>");
    33             Response.Write(gd3("ISIS") + "<br/>");
    34 
    35             GreetingDelegate gd4 = new GreetingDelegate(EnglishGreeting);
    36             gd4 += ChineseGreeting;//绑定语法
    37             Response.Write(gd4("Lucy") + "<br/>");
    38             gd4 -= ChineseGreeting;//取消绑定语法
    39             Response.Write(gd4("张三") + "<br/>");
    40         }
    41     }

    总结上述委托编程过程,可以从概念上形成以下几个名词:

        1. 委托定义:委托定义的作用是创建一个能够封装一类方法的类型。
        2. 委托字段:在委托编程中需要有一个委托字段,该字段可以用来创建委托对象。也可以把这个委托字段看成为是一个委托类型的变量(简称为委托变量)。于是,随时可以用一个方法对这个委托变量进行赋值。
        3. 委托实例:这是对委托字段的实例化,以形成委托对象。实例化的办法是用一个具有相同签名的方法名对委托变量进行赋值。实例化的作用是把委托字段与实例化方法关联在一起,形成委托对象。因此,无论何时,调用委托就是执行其实例化方法。
        4. 委托属性:可以用委托属性对外暴露委托对象。
        5. 执行/调用委托:执行或调用委托实际上是执行委托对象的实例化方法,执行/调用委托与执行规则的方法一样(送入方法参数、接收返回结果)。
     
    3、具有相同名称的接口
     1 public interface ImyInterface1
     2     {
     3         int Add();
     4     }
     5     public interface ImyInterface2
     6     {
     7         int Add();
     8     }
     9     public class MyClass : ImyInterface1, ImyInterface2  //继承接口
    10     {
    11         int ImyInterface1.Add()//显式接口成员实现
    12         {
    13             int x = 3;
    14             int y = 5;
    15             return x + y; 
    16         }
    17 
    18         int ImyInterface2.Add()//显式接口成员实现
    19         {
    20             int x = 3;
    21             int y = 5;
    22             int z = 7;
    23             return x + y+z;
    24         }
    25

    2016.4.13

    Response对象:程序响应对象
    打开TXT文件:
     1 if (!IsPostBack)//是否为第一次响应
     2             {
     3                 StreamReader sr = File.OpenText(Server.MapPath("大神别乱来.txt"));
     4                 string rt ;
     5                 while ((rt=sr.ReadLine())!= null)
     6                 {
     7                     Response.Write(rt + "<br/>");
     8                 }
     9                 sr.Close();
    10             }
    跳转页面:
        Response.Redirect("admin.aspx");
    设置缓存:
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));//设置缓存过期时间10s
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetValidUntilExpires(false);//缓存是否应忽略有由使缓存无效的客户端发送的标头
        lb1.Text = "当前系统时间" + DateTime.Now.ToString();//显示当前信息
    输出二进制图像
        Response.BinaryWrite(image);
     
    Request对象:程序请求对象
    获取客户端的IP地址:
        Request.UserHostAddress;
    获取客户端浏览器信息:
        浏览器平台: Request.Browser.Platform;
        浏览器类型:Request.Browser.Type;
        浏览器版本:Request.Browser.Version;
    站内搜索跳转:
        Response.Redirect("result.aspx?key=" + txtKey.Text.Trim());//将请求重定向到新URL并指定新URL
        Request.QueryString["key"].ToString();//获取HTTP查询字符串变量集合
     

    2016.4.14
    Application对象:全局变量应用对象
        该对象所存储的信息可以被多个客户使用,并且在整个Web应用程序运行期间持久的保存。一个客户停止了自己的应用程序,释放了Application对象的共享信息后,不影响其他客户。
        Application["变量名"] = 表达式;
    锁定与解锁:如果不加锁,当多个用户同时访问Application对象时会发生异常
        Application.Lock();
        Application.Unlock();
    全局应用程序类Global.asax:
        Application_Start事件:在首次创建新的会话之前发生
        Application_End事件:在用户退出Web应用程序且所有的会话都过期后发生,用于重要数据的存盘
        Session_Start事件:当新用户访问网站时会触发
        Session_End事件:当用户有效期失效时会触发

    Session对象:会话信息处理对象
    页面之间传值:
        Session["变量名"]="内容";
        Name = Session["变量名"];
    设置Session会话时间为一分钟,session对象的会话时间是以分钟为单位的:
        Session.Timeout = 1;
     
    Cookie对象:缓存对象
    1 Response.Cookies["username"].Expires = DateTime.Now.AddDays(30);
    2 Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays(30);
    3 Response.Cookies["username"].Value = txtname.Text.Trim();
    4 Response.Cookies["userpwd"].Value = txtpwd.Text.Trim();
    要将输入用户名的文本框的AutoPostBack属性设为true,这样,但用户输入用户名后,会自动回发到服务器,执行TextChanged事件下的代码
        Response.Cookies["username"].Expires:Cookie有效时间
        Response.Cookies["username"].Name:Cookie变量名
        Response.Cookies["username"].Value:Cookie变量值
     
    创建Cookie对象:
    1 HttpCookie newCookie = new HttpCookie("userIP");
    2 newCookie.Values.Add("IPaddress", UserIP);//将IP地址存储到Cookie对象中
    3 newCookie.Expires = DateTime.Now.AddMonths(1);//设置有效期为一个月
     
    Sever对象:服务器信息处理对象
    1 Server.MapPath("upload");//MapPath返回与Web服务器上指定虚拟路径相对性的绝对路径
    2 Server.MachineName;//获取服务器计算机的名称
    3 Server.UrlEncode;//对汉字进行编码
    4 Server.UrlDecode;//对汉字进行解码
  • 相关阅读:
    JavaScript 内置函数有什么?
    cursor(鼠标手型)属性
    用CSS制作箭头的方法
    CSS 的伪元素是什么?
    用CSS创建分页的实例
    CSS 按钮
    网页布局中高度塌陷的解决方法
    CSS 进度条
    DOM导航与DOM事件
    DOM 修改与DOM元素
  • 原文地址:https://www.cnblogs.com/xingye3327/p/5392782.html
Copyright © 2020-2023  润新知