zoukankan      html  css  js  c++  java
  • C#-构造函数

    构造函数(或称构造方法)

    构造方法用来创建对象,并且可以在构造函数中对对象进行初始化。

    作用:帮助我们初始化对象(给对象的每个属性依次的赋值)。

    不用构造函数的情况下,如下代码:

    //定义一个Students类
        public class Students
        {private string _name;
            public string Name//姓名属性
            {
                get { return _name; }
                set { _name = value; }
            }
            private char _gender;
            public char Gender//性别属性
            {
                get { return _gender; }
                set {
                    if (value != '' && value != '')
                    {
                        value = '';
                    }
                    _gender = value; }
            }
            private int _age;
            public int Age//年龄属性
            {
                get { return _age; }
                set {
                    if (value < 0 || value > 100)
                    {
                         value = 0;
                    }
                    _age = value; }
            }
            private int _chinese;
            public int Chinese//语文成绩属性
            {
                get { return _chinese; }
                set { _chinese = value; }
            }
            private int _math;
            public int Math//数学成绩属性
            {
                get { return _math; }
                set { _math = value; }
            }
    
            private int _english;
            public int English//英语成绩属性
            {
                get { return _english; }
                set { _english = value; }
            }
            public void SayHello()
            {
                Console.WriteLine($"我叫{this.Name},性别{this.Gender},年龄{this.Age}" );
            }
            public void ShowScore()
            {
                Console.WriteLine($"{this.Name}的语文成绩是{this.Chinese}分,数学成绩是{this.Math}分," +
                    $"英语成绩是{this.English}分,总成绩是{(this.Chinese+this.Math+this.English)}分。");
            }
        }
     //Main方法
            static void Main(string[] args)
            {
                //zsStudent
                Students zsStudent = new Students();
                zsStudent.Name = "ZS";
                zsStudent.Gender = '';
                zsStudent.Age = 22;
                zsStudent.Chinese = 80;
                zsStudent.Math = 80;
                zsStudent.English = 80;
                zsStudent.SayHello();
                zsStudent.ShowScore();
                Console.ReadKey();
    
                //lsStudent
                Students xlStudent = new Students();
                xlStudent.Name = "xl";
                xlStudent.Gender = '';
                xlStudent.Age = 21;
                xlStudent.Chinese = 90;
                xlStudent.Math = 90;
                xlStudent.English = 90;
                xlStudent.SayHello();
                xlStudent.ShowScore();
            }

    上面代码没有用到构造函数,在Main方法中调用Students类创建对象并给属性赋值时,当需要创建多个对象("zsStudent","xlStudent","lsStudent","wwStudent"……),你会发现,有一段给属性赋值的代码需要反复的写,这样写出来的代码就显得很臃肿,那么会不会有一种方法只需要一行代码就能完成创建对象和给属性赋值呢?

    有,我们这时候就需要用到构造函数

    在上面代码的基础上,我们加上构造函数,如下:

    //定义一个Students类
        public class Students
        {
            public Students(string name,char gender,int age,int chinese,int math,int english)//构造函数
            {
                this.Name = name;
                this.Gender = gender;
                this.Age = age;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
    
            private string _name;
            public string Name//姓名属性
            {
                get { return _name; }
                set { _name = value; }
            }
            private char _gender;
            public char Gender//性别属性
            {
                get { return _gender; }
                set {
                    if (value != '' && value != '')
                    {
                        value = '';
                    }
                    _gender = value; }
            }
            private int _age;
            public int Age//年龄属性
            {
                get { return _age; }
                set {
                    if (value < 0 || value > 100)
                    {
                         value = 0;
                    }
                    _age = value; }
            }
            private int _chinese;
            public int Chinese//语文成绩属性
            {
                get { return _chinese; }
                set { _chinese = value; }
            }
            private int _math;
            public int Math//数学成绩属性
            {
                get { return _math; }
                set { _math = value; }
            }
    
            private int _english;
            public int English//英语成绩属性
            {
                get { return _english; }
                set { _english = value; }
            }
            public void SayHello()
            {
                Console.WriteLine($"我叫{this.Name},性别{this.Gender},年龄{this.Age}" );
            }
            public void ShowScore()
            {
                Console.WriteLine($"{this.Name}的语文成绩是{this.Chinese}分,数学成绩是{this.Math}分," +
                    $"英语成绩是{this.English}分,总成绩是{(this.Chinese+this.Math+this.English)}分。");
            }
        }
    //Main方法
            static void Main(string[] args)
            {
                //zsStudent
                Students zsStudent = new Students("zs",'',22,80,80,80);
                zsStudent.SayHello();
                zsStudent.ShowScore();
                Console.ReadKey();
    
                //lsStudent
                Students xlStudent = new Students("xl",'',22,90,90,90);
                xlStudent.SayHello();
                xlStudent.ShowScore();
                Console.ReadKey();
            }

    上面代码中,我们在Students类中加了一段同名的函数,这就是构造函数,构造函数是一个比较特殊的方法。

     public Students(string name,char gender,int age,int chinese,int math,int english)//构造函数
            {
                this.Name = name;
                this.Gender = gender;
                this.Age = age;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }

    构造函数的特点:

    1)、构造函数没有返回值,连void也不能写
    2)、构造函数的名称必须跟类名一样。代码如下:

    //定义一个Students类
        public class Students
        {
            public Students(string name,char gender,int age,int chinese,int math,int english)//构造函数
            {

    3)、构造函数可以有参数,new对象的时候传递参数即可

    3)、创建对象的时候会执行构造函数。也就是说,在new这一步骤的时候,会执行构造函数。

     static void Main(string[] args)
            {
                //zsStudent
                Students zsStudent = new Students("zs",'',22,80,80,80);

    4)、构造函数的访问修饰符必须时public。如果访问修饰符不为public,创建对象时,将无法调用构造函数来初始化对象。

    5)、构造函数是可以有重载的。也就是有多个参数不同的构造函数。因为有时候,我们可能并不需要给所有的属性进行赋值。



    拓展:

    1)类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是无参数的,那个默认的无参数的构造函数都被干掉了。

    2)创建对象时,关键字new进行了三步工作:

    1、在内存中开辟一块空间

    2、在开辟的空间中创建对象

    3、调用对象的构造函数进行初始化对象

    3)关键字this

    1、代表当前类的对象
    2、在类当中显示的调用本类的构造函数  ,语法: “:this”(冒号加this),代码如下:

     public Students(string name, char gender,  int chinese, int math, int english)//构造函数
            {
                this.Name = name;
                this.Gender = gender;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
            public Students(string name, char gender, int age) : this(name,gender,age,0,0,0)
            {
               
            }
  • 相关阅读:
    OSX安装nginx和rtmp模块(rtmp直播服务器搭建)
    用runtime来重写Coder和deCode方法 归档解档的时候使用
    Homebrew安装卸载
    Cannot create a new pixel buffer adaptor with an asset writer input that has already started writing'
    OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
    让nginx支持HLS
    iOS 字典转json字符串
    iOS 七牛多张图片上传
    iOS9UICollectionView自定义布局modifying attributes returned by UICollectionViewFlowLayout without copying them
    Xcode6 iOS7模拟器和Xcode7 iOS8模拟器离线下载
  • 原文地址:https://www.cnblogs.com/ImOrange/p/10535991.html
Copyright © 2011-2022 走看看