C#基础复习之构造函数

构造函数

作用

帮助我们初始化对象,所谓初始化就是给对象的每个属性依次赋值

什么时候执行

创建对象的时候执行

 class Program
    {
        
        static void Main(string[] args)
        {

            Student zs = new Student(-96,65,89);//会进入对应的构造函数中
            
            Console.ReadKey();    
        }
        
    }
class Student
    {
        string _name;
        char _gender;
        int _age;
        int _chinese;
        int _math;
        int _english;
        public int Sum(int chinese,int math,int english)
        {
            int sum = 0;
            sum = chinese + math + english;
            return sum;
        }
        public Student()
        {

        }
        public Student(int age,int chinese,int english):this("张三",'难',-89,84,98,100)//这个构造函数去调用那个比较全的构造函数,关于这里我有个疑问,明明可以直接去调用那个比较全的构造函数非要写得绕来绕去得
        {

        }
        public Student(string name,char gender,int age,int chinese,int math,int english)
        {
            this.Name = name;
            if (gender != '男' && gender != '女') gender = '男';
            this.Gender = gender;
            if (age < 0 || age > 140) age = 0;
            this.Age = age;
            if (chinese < 0 || chinese > 100) chinese = 60;
            this.Chinese = chinese;
            if (math < 0 || math > 100) math = 60;
            this.Math = math;
            if (english < 0 || english > 100) english = 60;
            this.English = english;
        }
        public string Name { get => _name; set => _name = value; }
        public char Gender { get => _gender; set => _gender = value; }
        public int Age { get => _age; set => _age = value; }
        public int Chinese { get => _chinese; set => _chinese = value; }
        public int Math { get => _math; set => _math = value; }
        public int English { get => _english; set => _english = value; }
    }
    

特殊点

  1. 构造函数的函数名称与类名一样
  2. 构造函数没有返回值,连void都没有
  3. 类中会有一个默认的无参数的构造函数,如果你写了一个构造函数(不管有参数还是没有参数)之前的那个默认的构造函数都被干掉了
    构造函数可以重载,我认为只要是函数都可以重载,所以就没有把重载这一点算到特殊点中去

关键字new

1.new帮我们做了三件事请,暂时可以这么理解,之前我在一个网站上看到new做的事请很复杂

  1. 在内存中开辟一块空间
  2. 在开辟的空间中创建对象
  3. 调用类的构造函数对对象进行初始化
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/ab10df4c893f4956a3740e563de4e558.png

关键字this

作用

  1. 当前类的对象
  2. 调用当前类的构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blog
{
    internal class Test
    {
        string _name;
        int _age;
        double _chinese;
        double _math;
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }
        public int Age
        {
            set 
            {
                if (value < 0 && value > 100) value = 0;
                _age = value;
            }
            get { return _age; }
        }
        public double Chinese { get => _chinese; set => _chinese = value; }
        public double Math { get => _math; set => _math = value; }
        public Test(double chinese,double math):this("张三",10,78.5,89.99)//去调那个比较全的构造函数
        {
            //this.Chinese = chinese;
            //this.Math = math;//this表示当前类的对象
        }
        public Test(string name,int age,double chinese,double math)
        {
            this.Name = name;
            this.Age= age;
            this.Chinese= chinese;
            this.Math= math; 
        }
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值