• 技术文章 >后端开发 >C#.Net教程

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

    黄舟黄舟2017-02-10 15:23:46原创737

      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;
            }
        }

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

    //m.sbmmt.com/

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

     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

    以上就是C#基础知识整理:C#类和结构(2)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:C#基础知识整理:C#类和结构(1) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 从0自学C#04--特性和设计原则• C# 动态加载Dll• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 【c#教程】C# 属性(Property)• asp.net 图片验证码的HtmlHelper
    1/1

    PHP中文网