我正在制作第一个服务器客户端应用程序,我需要你的帮助。
客户端必须进行自我验证,然后才能玩一些游戏。
所以我有 USER 和 PLAYER 类:USER 包含所有注册用户,PLAYER 包含玩游戏的用户(有 PLAYER_GAME1、PLAYER_GAME2 等)。
在 USER 中,我有姓名、姓氏、id 等属性。
在 PLAYER 中,我需要有用户的属性以及点数、游戏时间等。
实际上:
public class USER
{
public string name, surname, ID, password, IP;
WALLET wallet;
bool login;
datetime lastAccess;
TcpClient socket;
Thread receiveMessages;//this receive message for log-in
...*other 30 proprieties*
public USER(string n,string s,string _id,string pw)
{
*inizialize all variables*
}
}
public class PLAYER
{
public USER user;
Thread receiveMessages;
int points;
bool online;
...*other proprieties*
public PLAYER(USER u)
{
user=u;
*inizialize all variables*
}
}
所以为了得到名字我必须做:
PLAYER p= new PLAYER(new USER(...));
string name=p.user.name;
我认为更聪明的是让 PLAYER 成为 USER 的子类,当用户想要玩游戏时,我用 player 的特性“扩展”类用户,所以我需要这样做:
public class USER
{
protected string name, surname, ID, password, IP;
WALLET wallet;
bool login;
datetime lastAccess;
TcpClient socket;
Thread receiveMessages;//this receive message for meneage the game
...*other 30 proprieties*
public USER(string n,string s,string _id,string pw)
{
*inizialize all variables*
}
}
public class PLAYER : USER
{
Thread receiveMessages;
...*other proprieties*
public PLAYER():base()// here, what could i do?
{
*inizialize all PLAYER variables*
}
}
所以为了得到名字,我会这样做:
PLAYER p= new PLAYER();
p=exixtingUser;
string name=p.name;
我知道 SUBCLASS=MAINCLASS 是不可能的,那么我该怎么做呢?