1

我有类Person,两个子类StaffStudent接口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;
        }
    }
}
4

2 回答 2

2

这是一个有点复杂的问题。所以,首先,我想指出你的 C# 的一些风格问题。

  • 你的database类是小写的,而其余的类是一致的。某些方法不一致(例如,您对某些方法使用惯用的 PascalCase,而对其他方法使用 camelCase 或小写)。
  • IPersonStaff实际上在这里没有任何意义,因为您可以将 a和Student周围的实例作为 a 传递,并Person以与现在基本相同的方式使用所有内容。在大多数情况下,您需要选择接口或抽象基类,而不是两者。
  • C# 有一个“属性”的概念,它基本上是一种方便的 getter 和 setter 语法。这优于公共字段(如在public string username您的database类中或您的public string idin 中Person),因为它允许您将支持字段的实现保持为私有。public string username { get; set; }如果您只想要一个默认实现,则此语法是。您可以将其扩展到更复杂的事情。例如,也许您想确保用户名被修剪。(1)
  • 次要的 nitpick,但通常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

如果任何部分不清楚,或者我的评估偏离标准,请随时提出澄清问题。

于 2013-05-11T09:15:51.960 回答
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();
        }
    }
于 2013-05-09T13:31:50.093 回答