• ASP.NET学习笔记2


    2016.4.7
    1、
    调用类时将类文件生成操作改为编译,然后
    using _73_ClassAndeObject.App_Code;
     
    2、类类型的成员变量都被自动设置为一个恰当的默认值
    bool类:false
    数值类:0
    string类:null
    char类:
    引用类型:null
     
    3、访问修饰符
    public:公共访问
    private:私有访问
    protected:只有子类才能访问,基类和外部代码都不能访问
    internal:不能在程序集外被任何类型访问
    protected internal:定义了一个对象的访问被限制在当前程序集,或者当前程序集从定义它的类所派生的类型中
    类型(类、接口、结构、枚举和委托)也可以带访问修符,但只能用public和internal
     
    4、new实例化
    类名 对象变量名=new 类名();
     
    5、对象访问:
    MyClass class1 = new MyClass();
    Response.Write(class1.add_x() + "<br/>");
    类访问:
    Response.Write(MyClass.add_y() + "<br/>");
     
    6、代码折叠整理
    #region name
    #endregion
     
    7、分布类(partial class)使用场合
    (1)处理大型项目时,可以将类拆分成几个文件,让多位程序员同时进行工作,不会因为只有一个文件造成彼此覆盖。
    (2)使用自动生成的源文件时,无需重新创建源文件即可将代码添加到类中。
     
    8、析构函数
    当某个类的实例被认为是不再有效,并符合析构条件时,调用该类的析构函数实现垃圾回收
    析构函数,释放非托管资源
            /// </summary>
            ~SqlDb()
            {
                try
                {
                    if (sqlcon != null)
                        sqlcon.Close();
                }
                catch{}
                try
                {
                    Dispose();
                }
                catch{}
            }
     
    9、使用静态属性可以直接通过类级别访问
    private static string companyName;//静态字段
    public static string Company//静态属性
    {
       get { return companyName; }
       set { companyName = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       Employee.Company = "Compay:";/
       Response.Write(Employee.Company);
    }

    2016.4.10

    1、base标签是用来指定起父类的。

    this关键字用来代表当前类。

    base则是用来代表父类

    例如两个类class A, class B

    classB继承自classA

    则,在classB中调用classA中的方法,则可用这样的格式:

    base.方法名

    Base使用范围:

    (1)调用基类上已被其他方法重写的方法。

    (2)指定创建派生类实例时应调用的基类构造函数。

     public class hBooks
        {
            public string bookName;
            public string ISBN;
            public hBooks(string bName,string bISBN)
            {
                this.bookName = bName;
                this.ISBN = bISBN;
            }
        }
        public class Sale : hBooks//定义派生类
        {
            public double bookSell;
            public Sale(string bName,string bISBN,double sell)//子类构造函数
                    :base(bName,bISBN)//基类构造函数
            {
                bookSell = sell;
            }
        }
     
    2、接口的实现
    public interface IDrawingObject
        {
            string Draw();
        }
    public class Line : IDrawingObject
        {
            public string Draw()
            {
                return "I'm Line.";
            }
        }
     Line l = new Line();
    Response.Write("直接调用类中的方法:"+l.Draw()+"<br/>");
    Response.Write("对接口中的方法调用1:" + ((IDrawingObject)l).Draw()+"<br/>");
    IDrawingObject myline = new Line();
    Response.Write("对接口中的方法调用2:" + myline.Draw());
     
    3、
    基类构造函数的执行要早于子类构造函数
    基类构造函数中对于虚方法的调用,实际调用的是子类中重写的虚方法
     

    2016.4.11
    1、实现多重继承
    public interface IPeople
        {
            string Name { get; set; }
            string Sex { get; set; }
        }
    public interface ITeacher : IPeople { string teach(); }//继承公共接口
    public interface IStudent : IPeople { string study(); }//继承公共接口
     
    public class person : IPeople, ITeacher, IStudent//多接口继承
        {
            string name = "";
            string sex = "";
            public string Name { get { return name; } set{ name = value; } }
            public string Sex { get { return sex; } set { sex = value; } }
     
            public string teach() { return "姓名:" + Name + " " + Sex ; }
            public string study() { return "姓名:" + Name + " " + Sex; }
        }
    protected void Page_Load(object sender, EventArgs e)
            {
                person per1 = new person();//实例化类对象
                ITeacher itea = per1;//使用派生类对象实例化接口ITeacher
                itea.Name = "Ondina";
                itea.Sex = "male";
                Response.Write(itea.teach()+"<br/>");
                IStudent istu = per1;//使用派生类对象实例化接口IStudent
                istu.Name = "Kanade";
                istu.Sex = "female";
                Response.Write(istu.study());
     
            }
     
    2、包含/委托模型
    class A
    {
    public int P{get;set;}
    }
    class B
    {
    public A a= new A();
    }
    使用: B.a.P;
     
    3、使用数组实现虚方法
    Drawingobj[] obj = new Drawingobj[4];
    obj[0] = new Line();
    obj[1] = new Circle();
    obj[2] = new Square();
    obj[3] = new Drawingobj();
    foreach (Drawingobj dobj in obj)
    {
       Response.Write(dobj.myDraw()+"<br/>");
    }
     
    4、重载
    方法名相同;参数列表必须不相同(参数类型、参数个数、参数顺序);返回值类型可以不相同
    不能通过访问权限、返回类型、抛出的异常进行重载,方法的异常类型和数目不会对重载在造成影响
    public static string myText(int x,string y){ return x +"-"+ y; }
    public static string myText(string y, int x){ return y + "-" + x; }
    public static string myText(int x, string y,double z) { return x + "-" + y + "-" + z; }
    string ol1 = OL.myText(1,"2");
    string ol2 = OL.myText("3", 4);
    string ol3 = OL.myText(5,"6",7.8);
    Response.Write(ol1+" "+ol2+" "+ol3);
     
  • 相关阅读:
    XML Schema
    Magento 2 instantiate object by Factory Objects
    Magento 2 Factory Objects
    UML类图与类的关系详解
    Magento add product attribute and assign to all group
    MyISAM 和InnoDB的区别
    Finding the Right EAV Attribute Table
    Implement Trie (Prefix Tree)
    Graph Valid Tree
    Maximum Subarray III
  • 原文地址:https://www.cnblogs.com/xingye3327/p/5380119.html
Copyright © 2020-2023  润新知