Jacklovely

导航

 

说明(2017-7-21 10:29:44):

1. 关于构造函数里的this和base,在网上查了很多资料,但都没有讲的很清楚的,或者是能让我看懂的,感觉都是叽叽歪歪,罗里吧嗦,磨磨唧唧的,有的直接就写个题目,然后贴上一段代码完事。

2. 很多人写博客,变量名起的很专业,只要是类,就要命名成xxxClass,只要是函数,就是xxxFun,比如父类就一定是BaseClass,子类就一定是SubClass。代码规范是好事,但是举例能生活化一点么?本来我就是要学习base的用法的,你给我一堆变量起名base,你可能觉得便于关联,其实并没有,只会看了眼花。

3. 还有喜欢输出英文的,比如

Console.WriteLine("sub class no argument constructor,actually call base constructor !!!");

一个字都不想看好吗?

我是来学base的,不是来学英语的,虽然也能看懂,但是心累好吗?输出这一坨有什么意义吗?我要的是一眼看过去就能明白什么意思的!你这几十行输出贴在那里,鬼知道你哪句输出了啥!

4. 只是吐槽一下,别人写篇文章也不容易,希望写文章的时候还是能照顾一下初学者、英语不好的等等。

5. 最后还是看了之前传智基础班的视频,稍微整理了一下。

首先是this,注释说的很明白了:

Student.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _01_构造函数
{
    public class Student
    {
        //一般的构造函数,里面是一堆参数,方便实例化的时候给参数赋值(俗称初始化)
        public Student(string name, int age, string gender)
        {
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }
        //带this的构造函数,借“主构造函数”(参数最多的)的参数来用,不需要的参数用任意相同类型的值来补,目的就是为了避免大括号里面每个参数再赋值一遍,造成代码冗余
        public Student(string name):this(name,1,"a")
        { }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public void SayHello()
        {
            Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
        }
        public void SayHello2()
        {
            Console.WriteLine("我叫{0}",this.Name);
        }
    }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _01_构造函数
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student("张三", 18, "");
            //s.Name = "张三";
            //s.Age = 18;
            //s.Gender = "男";
            s.SayHello();

            Student s2 = new Student("李四");
            s2.SayHello2();

            Console.ReadKey();

        }

    }
}

输出:

 然后是base,注释也很详细,为了方便把两个类写一起了:

 Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _02_构造函数base
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student("李四", 28, "");
            s.SayHello();
            Student s2 = new Student("王五", 22, "", 88, 23);
            s2.SayHello2();
            Console.ReadKey();
        }
        //Person类,这里有个地方要注意,之前偷懒不爱写字段,只写个属性。其实这样不好,因为如果只有属性,并且属性设为只读,那就没法改了,构造函数也不行。
        public class Person
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private int age;
            public int Age
            {
                get { return age; }
                //set { age = value; }
            }
            private string gender;
            public string Gender
            {
                get { return gender; }
                //set { gender = value; }
            }

            //父类构造函数,有name,age,gender三个参数
            public Person(string name, int age, string gender)
            {
                //这里用this代表本类Person,用来区分参数name,不然重名了。
                //在构造函数里将参数赋值给字段,这样就可以越过只读属性,直接修改字段了。
                this.name = name;
                this.age = age;
                this.gender = gender;
            }
        }
        //Student类,继承Person类。如果Person类内有自己写的有参构造方法,则Student类的构造方法必须要加:base继承这个构造方法!因为默认的Student:base(空)构造方法已经被干掉了!
        public class Student : Person
        {
            public int Chinese { get; set; }
            public int Math { get; set; }


            //如果不加:base会提示Person不包含0个参数的构造函数。
            public Student(string name, int age, string gender)//蒋坤:构造方法的定义
                : base(name, age, gender)//蒋坤:构造方法的调用//base里的参数,前面Student里面写什么,这里就写什么。
            { }
            public void SayHello()
            {
                Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
            }

            //子类构造函数有chinese,math两个,必须要加上父类的构造函数,参数如果没有值的话随便写
            public Student(string name, int age, string gender, int chinese, int math)
                : base(gender, age, name)//base里的参数,前面Student里面写什么,这里就写什么,前提是Person类有对应的这三个参数。因为这个Person类参数少,所以就写满三个就行,不然还要补空值。
            {
                this.Chinese = chinese;
                this.Math = math;
            }


            public void SayHello2()
            {
                Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,语文{3}分,数学{4}分。", this.Name, this.Age, this.Gender, this.Chinese, this.Math);
            }
        }
    }
}

输出:

 

总结:

学一个新的用法,一定要明确一点,这个用法的作用是什么?我为什么要用它?用了它有什么好处?(好吧这三点其实是一点)

首先,构造函数的作用是,给对象成员进行初始化

然后,构造函数this和base的作用就是:

1. this相当于继承了自己类的最完整的那个构造函数,目的就是省字数。

2. base相当于继承了父类的构造函数,目的也是省字数。

 

posted on 2017-07-21 10:52  Jacklovely  阅读(7263)  评论(2编辑  收藏  举报