一个C#类的简单了实例

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class lei : System.Web.UI.Page
{
    public class book
        {
            private string name;  //在此声明了book类的一个私有变量
            private string price; //同上
        public book()  //在此声明了book类的一个默认的构造函数
        {
            name="asp完全自学手册";
            price = "58";
        }
        
            public string Name //在此声明了一个Name属性 此属性使name变量的值可以在对象外对其赋值
            {
                get      //属性的只读方法
                {
                    return name;
                }
                set   //属性的读写权限
                {
                    name=value;
                }
                  
            }
              public string Price //price属性的读写权限 price属性有写权限
              {
              get
              {
                  return price;
              }
               set
               {
                   price = value;
               }

              }
            public string getinfo(string a, string b )  //声明一个方法
            {
                string c="";  //局部变量,外部不可访问
                c="书的名称是:" + a+"<br>书的价格是:"+b;
                return c;
            }
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        int i = 0;

        book onebook = new book(); //声明了onebook对象,此对象是book类的实例
        onebook.Name = "asp.net2.0教程";//为onebook的Name属性赋值
        onebook.Price = "58.5";//Price赋值
        string a=onebook.Name.ToString();
        string b=onebook.Price.ToString();
        string c=onebook.getinfo(a,b) ;//调用book类的getinfo方法,并添入两个值
        Response.Write(c + "<br>"); //输出结果结果为 "书的名称是:asp.net教程<br>书的价格是:58.5

        book onebook2 = new book();  //声明了onebook2对象,此对象是book类的实例
        string a2 = onebook2.Name.ToString(); //book类的默认构造函数
        string b2 = onebook2.Price.ToString();//同上
        string c2 = onebook2.getinfo(a2, b2); //调用book类的getinfo方法,并添入两个值
        Response.Write(c2);  //输出结果结果为 "书的名称是:asp完全自学手册<br>书的价格是:58
     
    }
}

你可能感兴趣的:(C#)