我有一个用于对象的类Person
。 Person类包含值Name,Last_Name,Age,最重要的是ID。
如何使用ID获取姓名或年龄等内容?代码:
// Person Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Person
{
private static int currentID;
protected int Age { get; set; }
protected string Name { get; set; }
protected string Name_Last { get; set; }
protected int ID { get; set; }
protected bool Teenager { get; set; }
public Person()
{
Name = "Default";
Name_Last = "Default";
ID = GetNextID();
Age = 0;
}
public Person(string name, int age)
{
this.Name = name;
this.Name_Last = Name_Last;
this.ID = GetNextID();
}
static Person()
{
currentID = 0;
}
protected int GetNextID()
{
return ++currentID;
}
public void Update(string name, int age, string LastName = "Default")
{
this.Name = name;
this.Name_Last = LastName;
this.Age = age;
}
public override string ToString()
{
return String.Format("Name: {0},"+getIsLastNameDiscription(" Lastname: ", ",")+" Age: {1}, isTeenager: "+isTeenager()+", CurrentID: {2}", this.Name, this.Age, this.ID);
}
private bool isLastname()
{
if (this.Name_Last != "Default")
return true;
else
return false;
}
private string getIsLastName()
{
if (this.isLastname())
return this.Name_Last;
else
return "";
}
private string getIsLastNameDiscription(string Stuffix = "", string EndStuffix = "")
{
if (this.isLastname())
return Stuffix+this.Name_Last+EndStuffix;
else
return "";
}
public string getIsTeenager(string TrueOutput = "true", string FalseOutput = "false")
{
if (this.isTeenager())
return TrueOutput;
else
return FalseOutput;
}
public bool isTeenager()
{
if(this.Age >= 13 && Age <= 18)
return true;
else
return false;
}
public string Discription()
{
return String.Format("{0} is {1} years old and he has the ID of {2}", this.Name,
this.Age,
this.ID);
}
}
// Program Class
class Program : Person
{
static void Main(string[] args)
{
Person Luke = new Person();
Luke.Update("Luke", 15);
Console.WriteLine(Luke.ToString());
Person Daniel = new Person();
Daniel.Update("Daniel", 14, "Jones");
Console.WriteLine(Daniel.ToString());
Person Aimee = new Person();
Aimee.Update("Aimee", 18, "Jones");
Console.WriteLine(Aimee.ToString());
Person Will = new Person();
Will.Update("William", 22, "Rae");
Console.WriteLine(Will.ToString());
Console.ReadLine();
}
}
答案 0 :(得分:0)
将您的Person
个对象添加到List,并使用id
list
get对象添加
答案 1 :(得分:0)
public class PersonRepo
{
static Dictionary<int, Person> people = new Dictionary<int, Person>();
public void Upsert(Person P)
{
if (people.ContainsKey(P.ID))
people[P.ID] = P;
else
people.Add(P.ID, P);
}
public Person Get(int ID)
{
if (!people.ContainsKey(ID))
return null;
return people[ID];
}
}
然后在你的主要功能中
public static void Main()
{
var people = new PersonRepo()
Person Luke = new Person();
Luke.Update("Luke", 15);
people.Upsert(Luke.ID,Luke);
Person Daniel = new Person();
Daniel.Update("Daniel", 14, "Jones");
people.Upsert(Daniel.ID,Daniel);
Person Aimee = new Person();
Aimee.Update("Aimee", 18, "Jones");
people.Upsert(Daniel.ID,Daniel);
Person Will = new Person();
Will.Update("William", 22, "Rae");
people.Upsert(Daniel.ID,Daniel);
// To get the person by id you can do
int id = 3;
var person = people.Get(id)
Console.WriteLine(person.ToString())
}
这没有见过编译器所以请原谅任何错别字
答案 2 :(得分:0)
你需要存储所有人,类似的东西:
class Program
{
static List<Person> persons = new List<Person>();
static void Main(string[] args)
{
Person Luke = new Person();
Luke.Update("Luke", 15);
Console.WriteLine(Luke.ToString());
Person Daniel = new Person();
Daniel.Update("Daniel", 14, "Jones");
Console.WriteLine(Daniel.ToString());
Person Aimee = new Person();
Aimee.Update("Aimee", 18, "Jones");
Console.WriteLine(Aimee.ToString());
Person Will = new Person();
Will.Update("William", 22, "Rae");
Console.WriteLine(Will.ToString());
persons.Add(Luke);
persons.Add(Daniel );
persons.Add(Aimee );
persons.Add(Will );
var founded = FindPersonById("xxx-x-xxxxx");
Console.ReadLine();
}
static Person FindPersonById(int Id)
{
return persons.FirstOrDefault(p => p.ID == Id);
}
}