我有课程Person
,两个子课程Staff
和Student
,接口IPerson
。我还有一个班级Database
和班级Gateway
。班级Database
有
private string name = "username";
和方法
public void getName() {return id;}
Staff和Student都有getName()方法。我需要通过网关从学生和员工类创建请求getName()到数据库。类网关必须检查Staff
(then return id
)或Student
(then return "Go away!"
)是否请求方法getName()。
任何人都可以帮助我。我正在考虑使用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;
}
}
}
答案 0 :(得分:2)
这是一个有点复杂的问题。所以,首先,我想指出你的C#的一些风格问题。
database
课程是小写的,而其他课程则是一致的。有些方法不一致(例如,对某些方法使用惯用的PascalCase,对其他方法使用camelCase或小写)。IPerson
实际上没有任何意义,因为您可以将Staff
和Student
的实例作为Person
传递,并以与现在基本相同的方式使用所有内容。在大多数情况下,您需要选择接口或抽象基类,而不是两者。public string username
类中的database
或public string id
中的Person
),因为它允许您将支持字段的实现保密。如果您只想要默认实现,则语法为public string username { get; set; }
。您可以将其扩展为更复杂的事物。例如,您可能希望确保修剪用户名。 (1)object
使用小写o。.ToString()
。 (2)(1)
private string m_username;
public string username {
get { return m_username; }
set { m_username = (value != null ? value.Trim() : value); }
}
(2)这些行是等价的。
Console.WriteLine(id + " notified that {1}", id, o.ToString());
Console.WriteLine("{0} notified that {1}", id, o);
现在问题。对我来说,听起来你想要不同类的不同行为。按照它的措辞,这听起来像访问/权限问题。根据数据存储的设置方式(在这种情况下,它看起来像代码中的常量,但您可以轻松地进行某种查询),您可以执行类似的操作...
[Flags]
public enum Permission {
None = 0,
GetName = 1
}
public abstract class Person {
/* ... */
public abstract Permission Permissions { get; }
}
public class Staff : Person {
/* ... */
public override Permission Permissions {
get { return Permission.GetName; }
}
}
public class Student : Person {
/* ... */
public override Permission Permissions {
get { return Permission.None; }
}
}
public class Database {
/* ... */
private Dictionary<string, string> NamesDatabase { get; set; }
public string getName(string id) {
// As a consequence of being managed by Gateway, assume that the caller has access
return NamesDatabase[id];
}
}
public class Gateway {
public string DoSomethingWithPerson(Person person, string desiredNamePersonId) {
if (person.Permissions.HasFlag(Permission.GetName)) {
Database db = new Database();
return db.getName(desiredNamePersonId);
}
return "go away!";
}
}
假设我们有Database
的构造函数:
public Database() {
NamesDatabase = new Dictionary<string, string>(2);
NamesDatabase["id1"] = "Student Amy";
NamesDatabase["id2"] = "Staff Mary";
}
这样Main
:
static void Main() {
Gateway gate = new Gateway();
Console.WriteLine("Student id1 looks up Staff id2: {0}", gate.DoSomethingWithPerson(new Student("id1"), "id2"));
Console.WriteLine("Staff id2 looks up Student id1: {0}", gate.DoSomethingWithPerson(new Staff("id2"), "id1"));
Console.ReadLine();
}
输出结果为:
Student id1 looks up Staff id2: go away!
Staff id2 looks up Student id1: Student Amy
如果任何部分不清楚,或者我的评估结果不合适,请随时提出澄清问题。
答案 1 :(得分:0)
我不确定这是否是你需要的。
static void Main(string[] args)
{
var gateway = new Gateway();
Console.WriteLine(gateway.DoSomethingWithPerson(new Staff(1)));
Console.WriteLine(gateway.DoSomethingWithPerson(new Student(1)));
}
public class Staff : Person
{
public Staff() { }
public Staff(int id) : base(id) { }
public override void Update(object o)
{
Console.WriteLine(ID + " notified that {1}", ID, o);
}
public override void UpdateMessage(object p)
{
Console.WriteLine(ID + " notified about new message in chat: {1}", ID, p);
}
public override string GetName()
{
return DataBase.GetName(ID);
}
}
public class Student : Person
{
public Student() { }
public Student(int id) : base(id) { }
public override void Update(object o)
{
Console.WriteLine(ID + " notified that {1}", ID, o);
}
public override void UpdateMessage(object p)
{
Console.WriteLine("Message for " + ID + " {1}", ID, p);
}
public override string GetName()
{
return "Go Away!";
}
}
public abstract class Person : IPerson
{
public int ID;
protected Person() { DataBase = new DataBase(); }
public abstract string GetName();
protected Person(int i) { ID = i; DataBase = new DataBase(); }
public abstract void Update(Object o);
public abstract void UpdateMessage(Object p);
public DataBase DataBase { get; set; }
}
public interface IPerson
{
void Update(Object o);
void UpdateMessage(Object p);
string GetName();
}
public class DataBase
{
public string USERNAME = "username";
private const string Name = "user details";
private const string Grade = "user grade";
public string GetName(int id)
{
// you should perform get something.
return Name;
}
public string GetGrade() { return Grade; }
}
//maybe call it facade
public class Gateway
{
public string DoSomethingWithPerson(IPerson person)
{
return person.GetName();
}
}