我有Person
類,兩個子類Staff
和Student
,接口IPerson
。另外我有一個類Database
和類Gateway
。 Database
類有如何執行方法
private string name = "username";
和方法
public void getName() {return id;}
工作人員和學生具有的getName()方法。我需要通過網關創建請求getName()從學生和職員類到數據庫。 Class Gateway必須檢查方法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;
}
}
}