C#class

面向对象的三大特性:

  封装、继承、多态(Encapsulation、Polymorphism、Inheritance);

C#class中的五种访问权限:

  public  可在这个程序集(命名空间)和引用他的其他命名空间使用

  private

  protected

  internal  和public类似,但只能在这个程序集使用

  protected internal  可在这个程序集(命名空间)和其他命名空间中的子类使用

类的访问权限(... class AA)只能是public/internal,默认是internal,即只能在这个工程中用这个类

类中访问权限的声明方式:(不声明默认为private)

  好像只能一个一个的声明:

  class AA

    {

        int a;

        public void set(int a)

        {

            this.a = a;

        }

        public int get()

        {

            return a;

        }

    }

  区别于C++:

  

class AA

{

    public://可以一次都声明

        int a;

        void set(int a)

        {

            this->a = a;

        }

        int get()

        {

            return a;

        }

};

 /*

  其他区别:

  1.  C++中this->???在C#中为this.???

  2.  C#中必须用new ... 来创建一个对象(AA t = new AA();),若不用new(AA t;)则不能使用它(AA t;t.set(1);)会报错;

       C++则可以(AA t;t.set(1);cout<<t.a<<endl;//输出1);

  3.  static用法不同,见http://www.cnblogs.com/wos1239/p/4382184.html

  以后发现了再加

*/

构造函数若不声明会有个默认的,在默认构造函数中变量的初始值为:

numeric(int,long,etc.)  0

bool           false

char           '\0' (null)

enum           0

reference         null

例如:

class AA
    {
        public int a;
        public char b;
        public double c;
        public bool d;
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            AA t = new AA();
            Console.WriteLine("{0} {1} {2} {3}",t.a,(int)t.b,t.c,t.d);
        }
    }

输出是:0 0 0 False

 

posted on 2015-03-31 19:54  wos1239  阅读(171)  评论(0编辑  收藏  举报

导航