0

我有类Person,两个子类StaffStudent接口IPerson。我也有一Database堂课Gateway。班级Database

private string id = "username";

和方法

public void getID() {return id;}

Staff 和 Student 都有 getID() 方法。网关必须检查方法 getID() 是否由Staff( return id) 或Student( 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
{
    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

1

我会is完全不鼓励链条。它违反了 Liskov。简而言之,这意味着很难在IPerson不修改Gateway以应对新类型的情况下添加新实现。或者它迫使开发人员从现有类型之一下降来实现一个新的IPerson,在这种情况下,接口与抽象Person类型相比有什么意义?

public class Gateway
{
 public string DoSomethingWithPerson(IPerson person)
 {
    return person.DoSomething();
 }
}

//then IPerson implementors like Student can provide the custom behaviour.
public class Student : Person
{
  public string DoSomething()
  { 
      return "Go Away!";
  }
  ...
}
于 2013-05-09T12:56:58.777 回答
0

我想你的网关类中有类似的东西:

public class Gateway
{
    public void DoSomethingWithPerson(IPerson person)
    {
        string id = person.getID();
        if (person is Student)
        {
            // do stuff for student, even cast is possible (Student) person
        }
        else if (person is Staff)
        {
            // do stuff for staff, even cast is possible (Staff) person
        }
    }
}

上面的代码假设getID()返回字符串 id (public void getID()在这个答案的那一刻你处于问题的状态)。


关于 is 关键字的注释

is关键字允许在运行时检查对象的类型。这适用于接口、类和结构。如果你有层次和独占条件,你必须小心,因为下面的代码:

if (person is Person)
{
    return;
}
else if (person is Student)
{
    // do student stuff
}

Student当您传递一个实例时,永远不会“做学生的事情” ,因为对于因为extendsperson is Person的任何实例都是如此。为了克服这个问题,在更抽象的检查之前使用具体的类型检查:StudentStudentPerson

if (person is Student)
{
    // do student stuff
}
else if (person is Person)
{
    return;
}
于 2013-05-09T11:10:17.370 回答