什么是“本”构造,它有什么作用 [英] what is 'this' constructor, what is it for

查看:142
本文介绍了什么是“本”构造,它有什么作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习过程中,我有一个问题,我还没有能够找到一个满意的答案。

I'm in the learning process and I have a question I havent been able to find a satisfactory answer for.

我需要它破败。我一直看到它,人们建议修复我使用它的code。我真的不知道究竟它的作用。如果有人会这么好心的给我就可以了基本的破败我会很高兴。

this I need a rundown on it. I keep seeing it and people have suggested fixes for my code that use it. I really have no idea what exactly it does. If someone would be so kind as to give me a basic rundown on it I would be really happy.

推荐答案

它被用来指代另一个构造在同一类。您可以使用它来继承另一个构造:

It's used to refer to another constructor in the same class. You use it to "inherit" another constructor:

public MyClass() {}

public MyClass(string something) : this() {}

在上面,调用第二个构造函数时,它首先执行该参数的构造函数,在执行本身之前。请注意,使用:这个()是相当于:基地(),但它指的是一个构造函数代替父类,同一类

In the above, when the second constructor is invoked, it executes the parameterless constructor first, before executing itself. Note that using : this() is the equivalent of : base(), except it refers to a constructor in the same class, instead of the parent class.

有一个关于构造这里的文章(MSDN ),它提供了一个实例:

There's an article about constructors here (MSDN), which provides a usage example:

public Employee(int annualSalary)
{
    salary = annualSalary;
}

public Employee(int weeklySalary, int numberOfWeeks)
    : this(weeklySalary * numberOfWeeks)
{
}

这篇关于什么是“本”构造,它有什么作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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