我有类Person
,两个子类Staff
和Student
接口IPerson
。我也有一Database
堂课Gateway
。班级Database
有
private string name = "username";
和方法
public void getName() {return id;}
Staff 和 Student 都有 getName() 方法。我需要通过网关创建从学生和教职员工类到数据库的请求 getName()。类网关必须检查方法 getName() 是否由Staff
( then return id
) 或Student
( then return "Go away!"
) 请求。谁能帮我解决这个问题。我正在考虑Gateway
用作Database
类的接口,但因为我只是想学习 C#,所以我真的不知道该怎么做。或者也许有更好的方法来做到这一点......请帮助谢谢
这是一些代码:
public class Staff : Person
{
public Staff() {}
public Staff(string id): base(id) {}
public override string getName()
{
throw new NotImplementedException();
}
public override void Update(object o)
{
Console.WriteLine(id + " notified that {1}", id, o.ToString());
}
public override void UpdateMessage(object p)
{
Console.WriteLine(id + " notified about new message in chat: {1}", id, p.ToString());
}
}
public class Student : Person
{
public Student() {}
public Student(string id): base(id) {}
public override string getName()
{
throw new NotImplementedException();
}
public override void Update(object o)
{
Console.WriteLine(id +" notified that {1}", id, o.ToString());
}
public override void UpdateMessage(object p)
{
Console.WriteLine("Message for " + id + " {1}", id, p.ToString());
}
}
public abstract class Person : IPerson
{
public string id;
public Person() { }
public abstract string getName();
public Person(string i) { this.id = i; }
public abstract void Update(Object o);
public abstract void UpdateMessage(Object p);
}
public interface IPerson
{
void Update(Object o);
void UpdateMessage(Object p);
string getName();
}
class database
{
public string username = "username";
private string name = "user details";
private string grade = "user grade";
public string getName(Object o)
{
if (o is Staff) { return name; }
else { return "Go away!"; }
}
public string getgrade() { return grade; }
}
public class Gateway
{
public void DoSomethingWithPerson(IPerson person)
{
string iD = person.getName();
if (person is Student)
{
return "go away!";
}
else if (person is Staff)
{
return name;
}
}
}