如何添加一个 属性 来表示一个初始化后不能更改的样本名称?
How to add a property to represent a sample name that may not be changed once initialized?
我正在尝试在我的 class 中创建一个 属性。 属性 初始化后无法更改,我需要做什么?
这些是实际的说明:
Create a class in the existing namespace, either in an existing code
file or in a new file, to represent the amount in pounds of dirt
sample. In this class (a) do not create constructors. (b) inherit the
sand class (to make use of the sand property). (c) add a property to
represent the sample name. This property may not be changed once
initialized. (d) add a property to represent and process assignments
the quantity of clay, with a minimum value of 0. (e) add methods to
return the weight of the sample, the percentage of sand in the sample
and the percentage of clay in the sample.
我在第 (c) 部分。我试图排除二传手。然后,我尝试使用只读,但它无法工作,因为我的 class 不能有构造函数。
public class AmountSand //parent class
{
public class AmountSand {
private double quantity;
public double Sand {
get {
return quantity;
}
set {
if (value >= 0) quantity = value;
}
}
}
public class AmountDirt: AmountSand { //part (b): inherited the parent class, AmountSand
private string name = null;
private double clay;
public string Name { //here is where the specific property starts
get {
return name;
}
set {
if (name == null)
name = value;
}
} //ends
public double Clay {
get {
return clay;
}
set {
if (value >= 0) clay = value;
}
}
取决于您希望从何处对其进行初始化。
编辑:抱歉,我没有读到您的 class 可能有 ctors,但为了完整起见,我会保留它。您的 class 不能有 ctors 似乎有点奇怪。请问为什么?
作者:
class MyClass
{
public MyClass()
{
Name = "Muhammed";
}
public MyClass(string newName)
{
Name = newName;
}
public string Name{get;}
}
如果您希望从 class 外部对其进行初始化,您的代码也不会太差。您甚至可以删除支持 属性。我会用
if(string.IsNullOrEmpty(Name))
而不是与 null 进行比较。
如果您希望通过 class 中的方法设置它:
public string Name{get; private set;}
字符串本质上已经是不可变的,因此您需要阐明您要实现的目标。
但是,如果您只是不希望任何其他东西能够设置 class 本身以外的值,那么您可以将设置访问器设为私有。
我正在尝试在我的 class 中创建一个 属性。 属性 初始化后无法更改,我需要做什么?
这些是实际的说明:
Create a class in the existing namespace, either in an existing code file or in a new file, to represent the amount in pounds of dirt sample. In this class (a) do not create constructors. (b) inherit the sand class (to make use of the sand property). (c) add a property to represent the sample name. This property may not be changed once initialized. (d) add a property to represent and process assignments the quantity of clay, with a minimum value of 0. (e) add methods to return the weight of the sample, the percentage of sand in the sample and the percentage of clay in the sample.
我在第 (c) 部分。我试图排除二传手。然后,我尝试使用只读,但它无法工作,因为我的 class 不能有构造函数。
public class AmountSand //parent class
{
public class AmountSand {
private double quantity;
public double Sand {
get {
return quantity;
}
set {
if (value >= 0) quantity = value;
}
}
}
public class AmountDirt: AmountSand { //part (b): inherited the parent class, AmountSand
private string name = null;
private double clay;
public string Name { //here is where the specific property starts
get {
return name;
}
set {
if (name == null)
name = value;
}
} //ends
public double Clay {
get {
return clay;
}
set {
if (value >= 0) clay = value;
}
}
取决于您希望从何处对其进行初始化。
编辑:抱歉,我没有读到您的 class 可能有 ctors,但为了完整起见,我会保留它。您的 class 不能有 ctors 似乎有点奇怪。请问为什么?
作者:
class MyClass
{
public MyClass()
{
Name = "Muhammed";
}
public MyClass(string newName)
{
Name = newName;
}
public string Name{get;}
}
如果您希望从 class 外部对其进行初始化,您的代码也不会太差。您甚至可以删除支持 属性。我会用
if(string.IsNullOrEmpty(Name))
而不是与 null 进行比较。
如果您希望通过 class 中的方法设置它:
public string Name{get; private set;}
字符串本质上已经是不可变的,因此您需要阐明您要实现的目标。
但是,如果您只是不希望任何其他东西能够设置 class 本身以外的值,那么您可以将设置访问器设为私有。