【文件属性】:
文件名称:不可变类型-python3+opencv3识别图片中的物体并截取的方法
文件大小:10.59MB
文件格式:PDF
更新时间:2021-07-10 13:22:12
.net之美 关键技术解析
1.4 不可变类型
在1.1.3简单类型中提到了string类型是一种特殊的引用类型,称作不可变类型
(Immutable Type)。本节就为大家详细地解释一下什么是不可变类型。
1.4.1 从类型设计谈起,Class还是Struct
假如要设计一个存储收信人地址的类型(Type),叫做Address,它包含了这样几个属
性:
Province 省
City 市
Zip 邮编
如果要对Zip格式进行控制(必须全为数字,且为6位),那么可以在其中添加一个
CheckZip()方法:
public class Address {
private string province;
private string city;
private string zip;
public string Province {
get { return province; }
set { province = value; }
}
public string City {
get { return city; }
set { city = value; }
}
public string Zip {
get { return zip; }
set {
CheckZip(value); // 验证格式
zip = value;
}
}
// 检测是不是正确的 zip
private void CheckZip(string value) {
string pattern = @"\d{6}";
if(!Regex.IsMatch(value, pattern))
throw new Exception("Zip is invalid! ");
}