[英]C# Class Add ID from one class to another
i have one class named with Animal, with Name, Type, Gender, and i have another class named with AnimalList, i need to know how can i add Animals to my AnimalList, this is my class animal(im in console application): Filename Animal.cs我有一个用 Animal 命名的类,名称,类型,性别,我有另一个用 AnimalList 命名的类,我需要知道如何将动物添加到我的 AnimalList,这是我的类动物(我在控制台应用程序中):文件名动物.cs
class Animal
{
public string Name {get; set;}
public string Type {get; set;}
public string Gender {get; set;}
public Person(string name, string type, string gender)
{
Name = name;
Type = type;
Gender = gender;
}
}
And my class AnimalList: FilenameAnimalList.cs还有我的类 AnimalList: FilenameAnimalList.cs
class AnimalList: List<Animal>
{
what should i do to add Animals to this list
}
shouldn't be better like this?不应该这样更好吗?
public new void Add(Animal value)
{
}
Why do you have the constructor for Person
in your Animal
class?为什么你的Animal
类中有Person
的构造函数?
Do like this:这样做:
var animals = new AnimalList();
var animal = new Animal();
animals.Add(animal);
You have to make your Animal class public:你必须公开你的 Animal 类:
public class Animal
{
}
Instantiate your AnimalList
class somewhere(for example in main method) and then add the instances of an Animal
class.在某处实例化你的AnimalList
类(例如在 main 方法中),然后添加一个Animal
类的实例。
for example:例如:
var animals = new AnimalList();
animals.Add(new Animal() {
Name = "",
Type = "",
Gender = ""});
First of all make your class Public And do this:首先让你的班级公开并这样做:
AnimalList animalList = new AnimalList();
Animal animal = new Animal();
animal.Name = "x";
animal.Type = "Y";
animal.Gender = "M/F";
animalList.add(animal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.