C#基础知识整理:C#类和结构(2)

来源:互联网 发布:免费永久个人域名注册 编辑:程序博客网 时间:2024/04/30 05:18

  1、什么是构造函数? 有哪些构造函数? 各个构造函数的定义、实现方法、注意事项?
所谓构造函数,就是一个方法,这个方法可以初始化对象,即运行完这个函数后,内存总开辟了一块该类的对象的空间。有三种:正常的构造函数,也就是实例化构造函数;私有构造函数;静态构造函数。
实例化构造器:

    public class Example    {        private string property1 = string.Empty;        private string property2 = @"hello";        private int property3 = 0;        public Example()//成员都是声明时的初始值,这种默认的构造器,也可以不写。        {        }        public Example(string p1, string p2, int p3)//传入的值初始化        {            this.property1 = p1;            this.property2 = p2;            this.property3 = p3;        }    }

私有构造器:
私有构造器,外部是不能访问的,那么如何实例化呢,参见单例模式,这里就是用了私有构造函数:

http://blog.csdn.net/yysyangyangyangshan/article/details/6962657

静态构造函数:
先看例子:

 public class StaticConstruct    {        static StaticConstruct()        {            Console.WriteLine(@"静态构造函数");        }        public StaticConstruct()        {            Console.WriteLine(@"实例化构造函数");        }        public StaticConstruct(string flage)        {            Console.WriteLine(@"带参构造函数");        }    }    class Program    {        static void Main(string[] args)        {            StaticConstruct strc = new StaticConstruct();            StaticConstruct strcValue = new StaticConstruct(string.Empty);            Console.ReadLine();        }    }

  结果:
   
    静态构造函数特点:静态构造函数中不允许出现访问修饰符;实例化的时候,首先自动调用静态构造函数,意即调用静态构造函数是不可控的;静态构造函数是无参的,并且一个类中只有一个;不能被继承。
  2、This关键字和Base关键字用途? 实现代码?
(1)、this关键字:
 this顾名思义,就是指本类中的意思,引用当前类的成员。当然如果程序在运行中,则可以精确地说,this指当前类的对象的成员,作用就是用来区分对象的。因为一个类可以有N个对象。不过在static类中不能使用this关键字,究其原因,无非是static不可能实例化多个对象,它只有一个,自然没必要去用this来区分对象了。一般常用如下:
 a、方法或构造函数中,同名变量。

     public class MyTestA    {        private string testA = string.Empty;        public MyTestA(string testA)        {            this.testA = testA;        }        public void Handler(string testA)        {            this.testA = testA;        }    }

b、get,set方法

    public class MyTestB    {        private string testB = string.Empty;        public string TestB        {            get             {                 return this.testB;            }            set             {                 this.testB = value;            }        }    }

c、将实例传递
比如,事件中

    public class MyTestC    {        public event EventHandler OnTestCEvent = null;        private void Send_OntestEvent(object sender,EventArgs e)        {            if (OnTestCEvent != null)            {                OnTestCEvent(sender, e);            }        }        private void TestEvent()        {            Send_OntestEvent(this, null);        }    }    public class MyTestD    {        MyTestC testC = new MyTestC();        public event EventHandler OnTestDEvent = null;        private void Send_OnTestDEvent(object sender, EventArgs e)        {            if (OnTestDEvent != null)            {                OnTestDEvent(sender, e);            }        }        public MyTestD()        {            testC.OnTestCEvent += new EventHandler(testC_OnTestEvent);        }        void testC_OnTestEvent(object sender, EventArgs e)        {            Send_OnTestDEvent(sender, e);        }    }    public class MyTestE    {        MyTestD testD = new MyTestD();        public MyTestE()        {            this.testD.OnTestDEvent += new EventHandler(testD_OnTestDEvent);        }        void testD_OnTestDEvent(object sender, EventArgs e)        {            MyTestC testC = sender as MyTestC;//通过MytestD将对象转了过来            if (testC != null)            {                //代码            }        }    }

(2)base关键字:
一般用于,子类访问父类。
一种是,重写父类方法时,

    public class ParentClass    {        public virtual void MethodA()        {            Console.WriteLine(@"基类的方法");        }    }    public class ChildClass : ParentClass    {        public override void MethodA()        {            base.MethodA();            Console.WriteLine("派生类方法");        }    }

另一种,子类调用父类构造函数,

    public class ParentClass    {        public ParentClass(string flage)        {            Console.WriteLine(@"基类构造函数");        }        public virtual void MethodA()        {            Console.WriteLine(@"基类的方法");        }    }    public class ChildClass : ParentClass    {        public ChildClass(string flage)            : base(flage)        {        }        public override void MethodA()        {            base.MethodA();            Console.WriteLine("派生类方法");        }    }

  3、什么是反射? 如何实现反射? 反射有何优缺点? 何时使用反射?
http://blog.csdn.net/yysyangyangyangshan/article/details/7028589

原创粉丝点击