简体   繁体   English

如何添加一个属性来表示样品名称,一旦初始化就不能更改?

[英]How to add a property to represent a sample name that may not be changed once initialized?

I am trying to make a property in my class. 我正在尝试在课堂上做一个财产。 What would I need to do where once the property is initialized, it cannot be changed? 在属性初始化后无法更改的地方该怎么办?

These are the actual instructions: 这些是实际的说明:

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. 在此类(a)中,不要创建构造函数。 (b) inherit the sand class (to make use of the sand property). (b)继承sand类(以利用sand属性)。 (c) add a property to represent the sample name. (c)添加一个表示样品名称的属性。 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. (d)添加一个属性以表示和处理分配的粘土量,最小值为0。(e)添加方法以返回样品的重量,样品中的沙子百分比和样品中的粘土百分比样品。

I am on part (c). 我在(c)部分。 I have tried to exclude setters. 我试图排除二传手。 Then, I've tried to use readonly, but it cannot work because my class cannot have constructors. 然后,我尝试使用只读,但由于我的类不能具有构造函数,因此它无法工作。

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;
   }
  }

Depends on from where you would like it to be initialized. 取决于您希望将其初始化的位置。

EDIT: sorry, i didn't read that your class could have ctors, but i'll keep this in for completeness. 编辑:对不起,我没有读过您的班级可能会有ctor,但是为了完整起见,我会保留下来。 It seems kinda weird that your class can't have ctors. 您的班级没有老师似乎有点不可思议。 May I ask why? 请问为什么?

From the ctor: 从ctor:

class MyClass
{
    public MyClass()
    {
        Name = "Muhammed";
    }

    public MyClass(string newName)
    {
        Name = newName;
    }

    public string Name{get;}
}

If you'd like it to be initialized from outside the class, your code is not too far off. 如果您希望从类外部对其进行初始化,则您的代码距离不太远。 You could even remove the backing property. 您甚至可以删除支持属性。 I'd use 我会用

if(string.IsNullOrEmpty(Name))

rather than comparing to null. 而不是与null比较。

if you'd like it to be set from a method inside your class: 如果您希望通过类中的方法进行设置:

public string Name{get; private set;}

Strings are already immutable by nature, so you need to clarify what you're trying to accomplish. 字符串本质上已经是不可变的,因此您需要阐明您要完成的工作。

However, if you simply don't want anything else to be able to set the value other than the class itself, then you can make the set accessor private. 但是,如果您只是不想让其他任何东西能够设置除类本身之外的值,则可以将设置的访问器设为私有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM