如何连接两个相同但在不同类中的变量
本文关键字:同类 变量 两个 何连接 连接 | 更新日期: 2023-09-27 18:26:17
好吧,我正在制作一些数据库,其中有4个类program.cs、athlete.cs、event.cs和venue.cs。他们希望我包括的方法之一是搜索运动员,以及显示赛事名称和场地名称。这必须在所有变量都是私有的情况下完成。
所以我想知道是否有办法将运动员类中的事件名称变量连接到事件类中的项目名称。
运动员级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Athlete
{
private string firstname, lastname, address, phonenumber;
private List<Event> eventlist = new List<Event>();
private Event athleteEvent;
public string athleteFirstname
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string athleteLastname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public string athleteAddress
{
get
{
return address;
}
set
{
address = value;
}
}
public string athletePhonenumber
{
get
{
return phonenumber;
}
set
{
phonenumber = value;
}
}
public Event eventA
{
get
{
return athleteEvent;
}
set
{
athleteEvent = eventlist[0].eventName;
}
}
}
事件类别
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Event
{
private string name, date, time, venueEvent;
private double fee;
private List<Athlete> athletes = new List<Athlete>();
public void addAthlete(Athlete a)
{
athletes.Add(a);// adds the athletes towards this class
}
public void displayAthletes()// method used for displaying athlete when requested
{
foreach (Athlete a in athletes) //Constructor
{
Console.WriteLine(a.athleteFirstname);
Console.WriteLine(a.athleteLastname);
}
}
public string eventName
{
get
{
return name;
}
set
{
name = value;
}
}
public string eventDate
{
get
{
return date;
}
set
{
date = value;
}
}
public string eventTime
{
get
{
return time;
}
set
{
time = value;
}
}
public double eventFee
{
get
{
return fee;
}
set
{
fee = value;
}
}
public string eventVenue
{
get
{
return venueEvent;
}
set
{
venueEvent = value;
}
}
}
这是我的搜索方法
#region Search Athlete
static void searchAthlete()
{
Console.WriteLine("Please enter which athlete you would like to find");
string searchChoice = Convert.ToString(Console.ReadLine());
for(int i = 0; i < athleteClass.ToArray().Length;i++) // goes through the athletes class to the end
{
if (searchChoice == athleteClass[i].athleteFirstname) // checks to see what has been entered matches any of the data in the athlete class
{
Console.WriteLine("Athlete First Name: " + athleteClass[i].athleteFirstname);
Console.WriteLine("Athlete Last Name: " + athleteClass[i].athleteLastname);
Console.WriteLine("Event Name: " + athleteClass[i].eventA);
Console.WriteLine("Venue Name: " + eventClass[i].eventVenue);
}
else
{
Console.WriteLine("No record was found");
}
//for (int e = 0; e < 2; e++)
//{
// if (athleteClass[e].eventA == eventClass[e].eventName) // checks
// {
// Console.WriteLine("Event name: " + eventClass[e].eventName);
// }
// for (int v = 0; v < 2; v++)
// {
// if (eventClass[v].eventVenue == venueClass[v].venueName)
// {
// Console.WriteLine("Venue Name: " + venueClass[v].venueName);
// }
// }
//}
}
}
#endregion
对于每个运动员,您都有一个由代码表示的事件列表。当他们添加新的运动员时,他们应该能够在控制台上添加事件列表,如{event1,event2…etc}。在搜索中,您只需调用Athlete对象Athlete.GetEventsList(),并打印出与该运动员相关的每个事件。我会做伪代码,这样你就可以自己学习了。
编辑首先,您需要为每位运动员保存事件。因此,您需要一个Set/Get方法来保存来自控制台的特定运动员的传入事件。一旦你有了运动员的活动列表,你就可以这样做了。
for each Athlete a in ListOfAthletes
{
get eventList for a
for each event in eventList
get event Name
//print out athlete Name and event Name
}