关于面向对象的问题
我有一个C#的问题是关于面向对象的, 有一个Club class(俱乐部类) 和一个 Registrant class(会员类), 俱乐部类里有俱乐部名字和电话号码,会员类里面有会员名和 Club property, 我已经在Program.cs 里面写了方法创建了游泳俱乐部会员和游泳俱乐部。
现在要求是在 Club class.cs 写一个method 方法,使得俱乐部成员可以分配到俱乐部, 如果俱乐部成员已经分配了俱乐部,会提示:""该成员已经分配到{俱乐部名字}俱乐部。" , 并且每个俱乐部不超过20个会员。
有没有哪位大神帮帮我,想不出来
这是Program.cs 文件的代码,是老师给的
```
static void Main(string[] args)
{
// Create 3 swimmers
Registrant swimmer1;
Registrant swimmer2;
Registrant swimmer3;
CreateSwimmers(out swimmer1, out swimmer2, out swimmer3);
//Create two clubs
Club club1;
Club club2;
CreateClubs(out club1, out club2);
//Assigne swimmers to club
AssigneSwimmersToClubs(swimmer1, swimmer2, swimmer3, club1, club2);
}
private static void AssigneSwimmersToClubs(Registrant swimmer1, Registrant swimmer2, Registrant swimmer3, Club club1, Club club2)
{
Console.WriteLine("*******Adding swimmers to clubs ******\n***********************************");
club1.AddSwimmer(swimmer1);
DisplayInfo("club1", club1.GetInfo());
DisplayInfo("swimmer1", swimmer1.GetInfo());
club2.AddSwimmer(swimmer2);
DisplayInfo("club2", club2.GetInfo());
DisplayInfo("swimmer2", swimmer2.GetInfo());
}
private static void CreateClubs(out Club club1, out Club club2)
{
club1 = new Club();
club1.PhoneNumber = 4164444444;
club1.Name = "NYAC";
DisplayInfo("club1", club1.GetInfo());
club2 = new Club("CCAC", 4165555555);
DisplayInfo("club2", club2.GetInfo());
}
private static void CreateSwimmers(out Registrant swimmer1, out Registrant swimmer2, out Registrant swimmer3)
{
swimmer1 = new Registrant("Bob Smith");
DisplayInfo("swimmer1", swimmer1.GetInfo());
swimmer2 = new Registrant();
DisplayInfo("swimmer2", swimmer2.GetInfo());
swimmer2.Name = "John Lee";
DisplayInfo("swimmer2", swimmer2.GetInfo());
swimmer3 = new Registrant("Ann Smith");
DisplayInfo("swimmer3", swimmer3.GetInfo());
}
public static void DisplayInfo(string firstLineMessage, string info)
{
Console.WriteLine("--{0}---------------------", firstLineMessage);
Console.WriteLine(info);
Console.WriteLine("--------------------------\n");
}
```
这是 Club.cs 的代码
```
class Club
{
private string name;
private long phoneNumber;
public Club()
{
}
public Club(string Name, long phoneNumber)
{
Name = name;
PhoneNumber = phoneNumber;
}
public string Name
{
set
{
name = value;
}
get
{
return name;
}
}
public long PhoneNumber
{
set
{
if (value < 10000000000 && value > 999999999)
{
phoneNumber = value;
}
}
get
{
return phoneNumber;
}
}
public string GetInfo()
{
string info;
info = string.Format($"\nClub Name: {Name}\nPhone Number: { PhoneNumber} \n Swimmers:");
return info;
}
}
```
这是Registrant.cs 的代码
```
class Registrant
{
private Club club;
private string name;
public Registrant()
{
}
public Registrant(string name)
{
Name = name;
}
public Club Club
{
set
{
club = value;
}
get
{
return club;
}
}
public string Name
{
set
{
name = value;
}
get
{
return name;
}
}
public string GetInfo()
{
string info;
info = string.Format("\nName: {0}\nClub: {1}", Name, Club?.Name ?? "not assigned.");
return info;
}
}
```
要求显示结果如下:
--swimmer1---------------------
Name: Bob Smith
Club: not assigned
--------------------------
--swimmer2---------------------
Name:
Club: not assigned
--------------------------
--swimmer2---------------------
Name: John Lee
Club: not assigned
--------------------------
--swimmer3---------------------
Name: Ann Smith
Club: not assigned
--------------------------
--club1---------------------
Name: NYAC
Phone: 4164444444
Swimmers:
--------------------------
--club2---------------------
Name: CCAC
Phone: 4165555555
Swimmers:
--------------------------
*******Adding swimmers to clubs ******
***********************************
--club1---------------------
Name: NYAC
Phone: 4164444444
Swimmers:
Bob Smith
--------------------------
--swimmer1---------------------
Name: Bob Smith
Club: NYAC
--------------------------
--club2---------------------
Name: CCAC
Phone: 4165555555
Swimmers:
John Lee
--------------------------
--swimmer2---------------------
Name: John Lee
Club: CCAC
--------------------------
Swimmer is registered with a different club
--swimmer3---------------------
Name: Ann Smith
Club: NYAC
--------------------------
--club1---------------------
Name: NYAC
Phone: 4164444444
Swimmers:
Bob Smith
Ann Smith
--------------------------
谢谢大家