如何在相同的 class 中使用 class 的属性,就像它们在 C# 中具有值一样
How to use the properties of a class in the same class like they have values in C#
我正在尝试在同一个 class 中使用 class 的 属性 来与另一个值进行比较。
但是,这个 属性 仍然是空的,因为我没有那个 class 的实例。请参阅下面的示例。
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string Name { get; set; }
public int Capacity { get; set; }
public string AddAnimal(Animal animal)
{
// Here I have tried to use the property Capacity.
// I have also tried to use "capacity" from the constructor.
// I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.
if (Capacity < AnimalsWhichCouldNotBeModified.Count)
{
return "The zoo is full.";
}
如何获取示例中使用的容量(仍未实例化且为空),以便检查动物是否超过动物园容量?
在你的第二个构造函数中(public Zoo()
不需要任何其他参数),你可以只设置你的 Capacity = 0
.
每次创建实例时,您要么使用第一个构造函数,您需要在其中手动提供 Capacity
,要么使用第二个构造函数自动设置 Capacity
到值 0.
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
Capacity = 0;
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string Name { get; set; }
public int Capacity { get; set; }
public string AddAnimal(Animal animal)
{
// Here I have tried to use the property Capacity.
// I have also tried to use "capacity" from the constructor.
// I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.
if (Capacity < AnimalsWhichCouldNotBeModified.Count)
{
return "The zoo is full.";
}
}
问题是您没有在 non-default 构造函数中初始化 AnimalsWhichCouldNotBeModified
。您可以将初始化添加到 non-default 构造函数中:
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
或从 non-default 中调用默认值:
public Zoo(string name, int capacity) : this()
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
或者,第三个选项,在 属性 的声明中提供一个初始值设定项:
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; } = new List<Animal>();
我个人更喜欢第三个选项,除非我需要在构造函数中使用更复杂的初始化代码。
注意Capacity
不初始化时,默认为0。这是因为基础 (compiler-generated) 支持字段被初始化为 0。默认情况下 AnimalsWhichCouldNotBeModified
和 Name
也是如此 null
。
我正在尝试在同一个 class 中使用 class 的 属性 来与另一个值进行比较。 但是,这个 属性 仍然是空的,因为我没有那个 class 的实例。请参阅下面的示例。
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string Name { get; set; }
public int Capacity { get; set; }
public string AddAnimal(Animal animal)
{
// Here I have tried to use the property Capacity.
// I have also tried to use "capacity" from the constructor.
// I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.
if (Capacity < AnimalsWhichCouldNotBeModified.Count)
{
return "The zoo is full.";
}
如何获取示例中使用的容量(仍未实例化且为空),以便检查动物是否超过动物园容量?
在你的第二个构造函数中(public Zoo()
不需要任何其他参数),你可以只设置你的 Capacity = 0
.
每次创建实例时,您要么使用第一个构造函数,您需要在其中手动提供 Capacity
,要么使用第二个构造函数自动设置 Capacity
到值 0.
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
Capacity = 0;
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string Name { get; set; }
public int Capacity { get; set; }
public string AddAnimal(Animal animal)
{
// Here I have tried to use the property Capacity.
// I have also tried to use "capacity" from the constructor.
// I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.
if (Capacity < AnimalsWhichCouldNotBeModified.Count)
{
return "The zoo is full.";
}
}
问题是您没有在 non-default 构造函数中初始化 AnimalsWhichCouldNotBeModified
。您可以将初始化添加到 non-default 构造函数中:
public Zoo(string name, int capacity)
{
Name = name;
Capacity = capacity;
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
或从 non-default 中调用默认值:
public Zoo(string name, int capacity) : this()
{
Name = name;
Capacity = capacity;
}
public Zoo()
{
AnimalsWhichCouldNotBeModified = new List<Animal>();
}
或者,第三个选项,在 属性 的声明中提供一个初始值设定项:
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; } = new List<Animal>();
我个人更喜欢第三个选项,除非我需要在构造函数中使用更复杂的初始化代码。
注意Capacity
不初始化时,默认为0。这是因为基础 (compiler-generated) 支持字段被初始化为 0。默认情况下 AnimalsWhichCouldNotBeModified
和 Name
也是如此 null
。