您的位置:首页 > 编程语言 > C#

C#学习笔记(三):面向对象和类

2006-11-03 17:35 169 查看
面向对象
    1、数据封装
    2、代码重用
    3、多态性
    4、重载
命名空间
    using [alias=]namespace;
声明自己的类
    1、封装数据
        private int count;
        ... ...
    2、构造和析构
        构造:public 类名(...) {...},可存在多个
        ~类名:析构,一般不需要
    3、方法
使用自己的类

    静态域:public static int field1;
    只读域:public readonly int field1;
属性
    修饰符 类型 标识符 {代码}
    private string name;
    public string Name
        {
            get{rerurn name;}
            set{name = value;}
        }
索引
    修饰符 类型 this[索引值]
    private int[] Array = new int[5];
    public int this[int index]
        {
            get   
                {
                    rerurn Array[index];
                }
            set
                {
                    Array[index] = value;
                }
        }
重载
    关键是参数不一样
    params:后面不能再有其他参数,因此一般是最后一个参数
    ref、out:实参前也必须加ref、out关键字,ref于out的区别在于out关键字不需要实参必须提前初始化
运算符重载
    语法:public static 返回类型 operator 运算符(参数){代码}
    数据转换也可重载
    语法:public static implicit operator 转换后的类型(被转换的数值)
                public static explicit operator 转换后的类型(被转换的数值)  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# string