概述
目前最大的限制是属性必须是可变的(译者注:即可写的),对象初始化器才能工作:它们首先调用对象的构造函数(本例中是默认的无参数构造函数),然后赋值给属性 setter。
仅初始化(init-only)属性解决了这个问题!它引入了一个 init 访问器,它是 set 访问器的变体,只能在对象初始化时调用:
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
有了这个声明,上面的客户端代码仍然是合法的,但是随后对 FirstName 和 LastName 属性的任何赋值都是错误的。
初始化(init) 访问器和只读(readonly)字段
因为 init 访问器只能在初始化期间调用,所以允许它们更改封闭类的只读(readonly)字段,就像在构造函数中一样。
public class Person
{
private readonly string firstName;
private readonly string lastName;
public string FirstName
{
get => firstName;
init => firstName = (value ?? throw new ArgumentNullException(nameof(FirstName)));
}
public string LastName
{
get => lastName;
init => lastName = (value ?? throw new ArgumentNullException(nameof(LastName)));
}
}
二、记录(record)
译者注:
原文中声明一个记录的 data class ** 联合关键字现在已经变成 record 关键字了,所以翻译过程中做了修正。
如果您想使单个属性不可变,那么仅初始化(init-only)属性是极好的。如果您想要整个对象是不可变的,行为像一个值,那么你应该考虑声明它为一个记录(record)
https://lanfc.com/ask/detail/25520.html
https://lanfc.com/ask/detail/25518.html
https://lanfc.com/ask/detail/25515.html
https://lanfc.com/ask/detail/25511.html
https://lanfc.com/ask/detail/25509.html
https://lanfc.com/ask/detail/25507.html
https://lanfc.com/ask/detail/25497.html
https://lanfc.com/ask/detail/25494.html
https://lanfc.com/ask/detail/25491.html
https://lanfc.com/ask/detail/25486.html
https://lanfc.com/ask/detail/25483.html
https://lanfc.com/ask/detail/25481.html
https://lanfc.com/ask/detail/25477.html
https://lanfc.com/ask/detail/25474.html
https://lanfc.com/ask/detail/25469.html
https://lanfc.com/ask/detail/25467.html
https://lanfc.com/ask/detail/25461.html
https://lanfc.com/ask/detail/25459.html
https://lanfc.com/ask/detail/25455.html
https://lanfc.com/ask/detail/25453.html
https://lanfc.com/ask/detail/25448.html
https://lanfc.com/ask/detail/25444.html
https://lanfc.com/ask/detail/25441.html
https://lanfc.com/ask/detail/25438.html
https://lanfc.com/ask/detail/25435.html
https://lanfc.com/ask/detail/25432.html
https://lanfc.com/ask/detail/25426.html
https://lanfc.com/ask/detail/25423.html
https://lanfc.com/ask/detail/25420.html
https://lanfc.com/ask/detail/25416.html
https://lanfc.com/ask/detail/25412.html
https://lanfc.com/ask/detail/25407.html
https://lanfc.com/ask/detail/25405.html
https://lanfc.com/ask/detail/25402.html
https://lanfc.com/ask/detail/25399.html
https://lanfc.com/ask/detail/25396.html
https://lanfc.com/ask/detail/25393.html
https://lanfc.com/ask/detail/25391.html
https://lanfc.com/ask/detail/25386.html
https://lanfc.com/ask/detail/25383.html
https://lanfc.com/ask/detail/25380.html
https://lanfc.com/ask/detail/25375.html
https://lanfc.com/ask/detail/25373.html
https://lanfc.com/ask/detail/25371.html
https://lanfc.com/ask/detail/25365.html
https://lanfc.com/ask/detail/25362.html
https://lanfc.com/ask/detail/25359.html
https://lanfc.com/ask/detail/25354.html
https://lanfc.com/ask/detail/25352.html
https://lanfc.com/ask/detail/25350.html
https://lanfc.com/ask/detail/25348.html
https://lanfc.com/ask/detail/25345.html
https://lanfc.com/ask/detail/25340.html
https://lanfc.com/ask/detail/25337.html
https://lanfc.com/ask/detail/25334.html
https://lanfc.com/ask/detail/25331.html
https://lanfc.com/ask/detail/25327.html
https://lanfc.com/ask/detail/25325.html
https://lanfc.com/ask/detail/25322.html
https://lanfc.com/ask/detail/25318.html
https://lanfc.com/ask/detail/25316.html
https://lanfc.com/ask/detail/25313.html
https://lanfc.com/ask/detail/25310.html
https://lanfc.com/ask/detail/25308.html
https://lanfc.com/ask/detail/25306.html
https://lanfc.com/ask/detail/25304.html
https://lanfc.com/ask/detail/25302.html
https://lanfc.com/ask/detail/25299.html
https://lanfc.com/ask/detail/25296.html
https://lanfc.com/ask/detail/25293.html
https://lanfc.com/ask/detail/25291.html
https://lanfc.com/ask/detail/25289.html
https://lanfc.com/ask/detail/25287.html
https://lanfc.com/ask/detail/25281.html
https://lanfc.com/ask/detail/25278.html
https://lanfc.com/ask/detail/25276.html
https://lanfc.com/ask/detail/25275.html
https://lanfc.com/ask/detail/25273.html
https://lanfc.com/ask/detail/25271.html
https://lanfc.com/ask/detail/25269.html
https://lanfc.com/ask/detail/25266.html
https://lanfc.com/ask/detail/25265.html
https://lanfc.com/ask/detail/25263.html
https://lanfc.com/ask/detail/25261.html
https://lanfc.com/ask/detail/25259.html
https://lanfc.com/ask/detail/25255.html
https://lanfc.com/ask/detail/25253.html
https://lanfc.com/ask/detail/25250.html
https://lanfc.com/ask/detail/25248.html
https://lanfc.com/ask/detail/25245.html
https://lanfc.com/ask/detail/25243.html
https://lanfc.com/ask/detail/25235.html
https://lanfc.com/ask/detail/25233.html
https://lanfc.com/ask/detail/25229.html
https://lanfc.com/ask/detail/25225.html
https://lanfc.com/ask/detail/25221.html
https://lanfc.com/ask/detail/25218.html
https://lanfc.com/ask/detail/25215.html
https://lanfc.com/ask/detail/25213.html
https://lanfc.com/ask/detail/25211.html
https://lanfc.com/ask/detail/25210.html
https://lanfc.com/ask/detail/25207.html
https://lanfc.com/ask/detail/25204.html
https://lanfc.com/ask/detail/25202.html
https://lanfc.com/ask/detail/25199.html
https://lanfc.com/ask/detail/25195.html
https://lanfc.com/ask/detail/25191.html
https://lanfc.com/ask/detail/25188.html
https://lanfc.com/ask/detail/25186.html
https://lanfc.com/ask/detail/25184.html
https://lanfc.com/ask/detail/25180.html
https://lanfc.com/ask/detail/25179.html
https://lanfc.com/ask/detail/25177.html
https://lanfc.com/ask/detail/25175.html
https://lanfc.com/ask/detail/25174.html
https://lanfc.com/ask/detail/25173.html
https://lanfc.com/ask/detail/25171.html
https://lanfc.com/ask/detail/25168.html
https://lanfc.com/ask/detail/25165.html
https://lanfc.com/ask/detail/25163.html
https://lanfc.com/ask/detail/25159.html
https://lanfc.com/ask/detail/25157.html
https://lanfc.com/ask/detail/25154.html
https://lanfc.com/ask/detail/25151.html
https://lanfc.com/ask/detail/25147.html
https://lanfc.com/ask/detail/25145.html
https://lanfc.com/ask/detail/25140.html
https://lanfc.com/ask/detail/25135.html
https://lanfc.com/ask/detail/25132.html
https://lanfc.com/ask/detail/25128.html
https://lanfc.com/ask/detail/25125.html
https://lanfc.com/ask/detail/25122.html
https://lanfc.com/ask/detail/25119.html
https://lanfc.com/ask/detail/25115.html
https://lanfc.com/ask/detail/25112.html
https://lanfc.com/ask/detail/25109.html
https://lanfc.com/ask/detail/25102.html
https://lanfc.com/ask/detail/25100.html
https://lanfc.com/ask/detail/25098.html
https://lanfc.com/ask/detail/25094.html
https://lanfc.com/ask/detail/25091.html
https://lanfc.com/ask/detail/25088.html
https://lanfc.com/ask/detail/25085.html
https://lanfc.com/ask/detail/25084.html
https://lanfc.com/ask/detail/25082.html
https://lanfc.com/ask/detail/25080.html
https://lanfc.com/ask/detail/25079.html
https://lanfc.com/ask/detail/25077.html
https://lanfc.com/ask/detail/25075.html
https://lanfc.com/ask/detail/25074.html
https://lanfc.com/ask/detail/25073.html
https://lanfc.com/ask/detail/25071.html
https://lanfc.com/ask/detail/25068.html
https://lanfc.com/ask/detail/25067.html
https://lanfc.com/ask/detail/25066.html
https://lanfc.com/ask/detail/25065.html
https://lanfc.com/ask/detail/25064.html
https://lanfc.com/ask/detail/25063.html
https://lanfc.com/ask/detail/25062.html
https://lanfc.com/ask/detail/25060.html
https://lanfc.com/ask/detail/25059.html
https://lanfc.com/ask/detail/25057.html
https://lanfc.com/ask/detail/25055.html
https://lanfc.com/ask/detail/25054.html
https://lanfc.com/ask/detail/25053.html
https://lanfc.com/ask/detail/25051.html
https://lanfc.com/ask/detail/25049.html
https://lanfc.com/ask/detail/25048.html
https://lanfc.com/ask/detail/25047.html
https://lanfc.com/ask/detail/25046.html
https://lanfc.com/ask/detail/25045.html
https://lanfc.com/ask/detail/25044.html
https://lanfc.com/ask/detail/25043.html
https://lanfc.com/ask/detail/25042.html
https://lanfc.com/ask/detail/25041.html
https://lanfc.com/ask/detail/25039.html
https://lanfc.com/ask/detail/25037.html
https://lanfc.com/ask/detail/25034.html
https://lanfc.com/ask/detail/25033.html
https://lanfc.com/ask/detail/25032.html
https://lanfc.com/ask/detail/25031.html
https://lanfc.com/ask/detail/25029.html
https://lanfc.com/ask/detail/25028.html
https://lanfc.com/ask/detail/25027.html
https://lanfc.com/ask/detail/25026.html
https://lanfc.com/ask/detail/25025.html
https://lanfc.com/ask/detail/25024.html
https://lanfc.com/ask/detail/25022.html
https://lanfc.com/ask/detail/25020.html
https://lanfc.com/ask/detail/25019.html
https://lanfc.com/ask/detail/25018.html
https://lanfc.com/ask/detail/25016.html
https://lanfc.com/ask/detail/25015.html
https://lanfc.com/ask/detail/25013.html
https://lanfc.com/ask/detail/25011.html
https://lanfc.com/ask/detail/25010.html
https://lanfc.com/ask/detail/25007.html
https://lanfc.com/ask/detail/25006.html
https://lanfc.com/ask/detail/25005.html
最后
以上就是平常黑夜为你收集整理的Node.js 是一个基于Chrome JavaScript 运行时建立的的全部内容,希望文章能够帮你解决Node.js 是一个基于Chrome JavaScript 运行时建立的所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复