C# 谈谈Interface和通过Interface传递web页面数据

接口:描述可属于任何类或结构的一组相关功能,通过interface关键字来声明;
接口只包含方法、委托或事件和属性的签名(接口包含的成员)、不能包含字段(因为字段是包含数据的)。方法的实现是“继承”接口的类中完成的;
接口可以包含的成员的访问修饰符只能且默认为public;
一个接口可以从一个或多个基接口继承;
接口类似于抽象基类:继承接口的任何非抽象类型都必须实现接口的所有成员;
当基类型列表包含基类和接口时,基类必须是列表中的第一项;
实现接口的类可以显式实现该接口的成员,显示实现的成员不能通过类实例访问,而只能通过接口实例访问;
类和结构可以按照类继承基类或结构的类似方式继承接口;但注意:
类或结构可继承多个接口;
类或结构继承接口时,仅继承方法名称和签名,因为接口本身不包含实现;
接口和接口成员是抽象的(但不用写出abstract关键字);接口不提供默认实现;

接口是一种规划(为你定义出一系列的规则和任务,但不去实现它);

先看一个实例:

interface IPoint
{
// Property signatures:
int x
{
get;
set;
}

int y
{
get;
set;
}
}

class Point : IPoint
{
// Fields:
private int _x;
private int _y;

// Constructor:
public Point(int x, int y)
{
_x
= x;
_y
= y;
}

// Property implementation:
public int x
{
get
{
return _x;
}

set
{
_x
= value;
}
}

public int y
{
get
{
return _y;
}
set
{
_y
= value;
}
}
}

class MainClass
{
static void PrintPoint(IPoint p)
{
Console.WriteLine(
"x={0}, y={1}", p.x, p.y);
}

static void Main()
{
Point p
= new Point(2, 3);
Console.Write(
"My Point: ");
PrintPoint(p);
}
}
// Output: My Point: x=2, y=3

上面是一个简单的接口实现,如果在一个Web网页上的.CS文件中继承一个接口,如下:

先定义接口:

  public interface Iuser
    {
        string UserName { set; get; }
        string Role { set; get; }
        string Age { set; get; }
        string Sex { set; get; }
        bool Userlogin(string Name,string PassWord);

    }

新建一个页面Login.aspx,代码如下;

<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="lblPassWord" runat="server" Text="PassWord"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtPassWord" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                     </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
                        Text="Submit" />
                </td>
            </tr>
            <tr>
                <td class="style2">
                     </td>
                <td>
                    <asp:Label ID="lblMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>

Login.aspx.cs代码如下:

    public partial class Login : Iuser
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private string _Age;
        private string _Role;
        private string _Sex;
        private string _UserName;

        #region Iuser Members

        public string Age
        {
            get
            {
                return _Age;
            }
            set
            {
                _Age = value;
            }
        }

        public string Role
        {
            get
            {
                return _Role;
            }
            set
            {
                _Role = value;
            }
        }

        public string Sex
        {
            get
            {
                return _Sex;
            }
            set
            {
                _Sex = value;
            }
        }

        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }


        public bool Userlogin(string Name, string PassWord)
        {
            return true;
        }
        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Userlogin(txtUserName.Text, txtPassWord.Text))
            {
                lblMessage.Text = "login Successful!";
                SetUserInfo(txtUserName.Text);
            }
            else
            {
                lblMessage.Text = "Login  fail!";
            }
        }
        protected void SetUserInfo(string Name)
        {
            Age = "25";
            Role = "Visit";
            Sex = "Man";
            UserName = "spring yang";
        }

    }

然后新建一个UsingUser.cs类;

代码如下:

    public class UsingUser : WebPart, Iuser
    {
        private string _Age;
        private string _Role;
        private string _Sex;
        private string _UserName;

        #region Iuser Members

        public string Age
        {
            get
            {
                return _Age;
            }
            set
            {
                _Age = value;
            }
        }

        public string Role
        {
            get
            {
                return _Role;
            }
            set
            {
                _Role = value;
            }
        }

        public string Sex
        {
            get
            {
                return _Sex;
            }
            set
            {
                _Sex = value;
            }
        }

        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }


        public bool Userlogin(string Name, string PassWord)
        {
            return true;
        }
        #endregion
        private void AddControlToWebPart()
        {
            Type controlType = Type.GetType("ASP.web_Login_aspx,webinterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5");
            System.Web.UI.UserControl userControl = (System.Web.UI.UserControl)this.Page.LoadControl(controlType, null);
            Iuser iuserProperty = userControl as Iuser;

            Age = iuserProperty.Age;
            Role = iuserProperty.Role;
            Sex = iuserProperty.Sex;
            UserName = iuserProperty.UserName;
           
            this.Controls.Add(userControl);
        }
    }

这样就可以调用页面内容的数据了,通过接口传递页面的数据就这样完成了,你可以在UsingUser.cs做你想要的操作了.


posted @ 2011-03-16 15:50  spring yang  阅读(4105)  评论(23编辑  收藏  举报