c#模式子类

本文关键字:子类 模式 | 更新日期: 2023-09-27 18:12:44

我有类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;
        }
    }
}

c#模式子类

我完全不赞成is链。它违反了利斯科夫定理。简而言之,这意味着如果不修改Gateway来处理新类型,就很难添加新的IPerson实现。或者它迫使开发人员从现有的类型之一下降到实现新的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!";
  }
  ...
}

我想你在你的Gateway类中也有类似的东西:

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实例时,

将永远不会"做学生的事情",因为Student扩展了Person,所以person is PersonStudent的任何实例都是true。为了克服这个问题,在之前使用具体的类型检查,更抽象的类型检查:

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