“这个"的作用是什么?构造函数中的关键字? [英] What is the function of the "this" keyword in a constructor?

查看:24
本文介绍了“这个"的作用是什么?构造函数中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才在看 MSDN 的示例代码,偶然发现:

I was looking at sample code from MSDN just now and came accross:

namespace IListSourceCS
{
    public class Employee : BusinessObjectBase
    {
        private string      _id;
        private string      _name;
        private Decimal     parkingId;

        public Employee() : this(string.Empty, 0) {} // <<--- WHAT IS THIS???
        public Employee(string name) : this(name, 0) {}

推荐答案

它使用该签名调用该类中的另一个构造函数.它是根据其他构造函数实现构造函数的一种方式.base 也可用于调用基类构造函数.您必须有一个与此匹配的签名构造函数才能使其工作.

It calls the other constructor in that class with that signature. Its a way of implementing the constructor in terms of other constructors. base can also be used to call the base class constructor. You have to have a constructor of the signature that matches this for it to work.

这篇关于“这个"的作用是什么?构造函数中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆