假设我创建了一个名为Person的类,其中包含名称,出生年份和性别,例如:
class Person{
public string name;
public int year;
public string gender;
public Person(string _name, int _year, string _gender)
{
name= _name;
year= _year;
gender= _gender;
}
}
使用那个班级,我在我的主人中创造了3个人,让我们说:
Person person1 = new Person("Bob",1992,"M");
Person person2 = new Person("Gilbert", 1984, "M");
Person persone = new Person("hank" 1989, "F");
我的问题是:是否可以创建一个方法,该方法需要一年的时间作为参数,并仅基于该方法找到一个人的年龄和姓名?
这就是我所做的,但如果只有3个人的话,那就不好了:
public string age(int ParamAge)
{
string message = "";
if (ParamAge == 1992)
message = "Bob is " + (DateTime.Now.Year - ParamAge) + " years old.";
if (ParamAge == 1984)
message = "Gilbert is " + (DateTime.Now.Year - ParamAge) + " years old.";
if (ParamAge == 1989)
message = "Hank is " + (DateTime.Now.Year - ParamAge) + " years old.";
return message;
}
答案 0 :(得分:1)
您应该将所有Person
个实例添加到List<Person>
。使用linq,您可以根据标准选择一个人。
为了简单起见,我使用了您的代码:
// this is what you know, creating persons.
Person person1 = new Person("Bob","McGee",1992,"M"); // <---- NOTE? four parameters???? (you code would not compile....)
Person person2 = new Person("Gilbert", 1984, "M");
Person persone = new Person("hank" 1989, "F");
// create a list that contains Persons
List<Person> list = new List<Person>();
// add those persons to the list.
list.Add(person1);
list.Add(person2);
list.Add(persone);
// with the FirstOrDefault() extension method you can select a person.
Person person = personList.FirstOrDefault(item => item.year == year);
// create a message
string message;
// check if the person is a valid instance, because FirstOrDefault will return the default(Person) if none of the persons mathes the criteria. (default == null)
if(person != null)
message = Person.name + " is " + (DateTime.Now.Year - person.year).TotalYears + " years old.";
else
message = "Could not find anyone";
// show the message.
MessageBox.Show(message);
简而言之,您不需要为要添加的人创建一个Person声明。
例如:
list.Add(new Person("Gilbert", 1984, "M"));
答案 1 :(得分:0)
除了对+/-一年的准确性的关注之外,我会让班级为你进行计算。
public class Person
{
public string Name { get; set; }
public int Year { get; set; }
public string Gender { get; set; }
public int RelativeAge
{
get { return DateTime.Today.Year - Year; }
}
public Person(string name, int year, string gender)
{
Name = name;
Year = year;
Gender = gender;
}
public string AgeStatement()
{
return string.Format("{0} is {1} {2} old.", Name, RelativeAge, MakePlural("year", RelativeAge));
}
/// <summary>
/// Add an S to values over one.
/// </summary>
/// <param name="element">Item to be plural</param>
/// <param name="value"></param>
/// <returns></returns>
public string MakePlural(string element, int value)
{
return element + ((value > 1) ? "s" : "");
}
}
以下是验证结果的测试:
[TestClass]
public class PersonUnitTests
{
[TestMethod]
public void TestPersonRelativeAge()
{
var p = new Person("Jane", 1986, "F");
Assert.AreEqual(p.RelativeAge, 30);
}
[TestMethod]
public void TestAgeStatement()
{
var p = new Person("Jane", 1986, "F");
Assert.AreEqual(p.AgeStatement(), "Jane is 30 years old.");
}
}