在两个子类

时间:2016-05-17 17:06:47

标签: c# abstract

我有两个子类和一个父类。

父类有抽象方法,它有一个类作为参数。 Child类实现parent的抽象方法,它有另一个类作为参数。但是子类方法会出错:

  

"没有实现继承的抽象成员"。

以下是代码:

abstract class StudentBizz
{
    public abstract Boolean enrollNewStudent(Student stuObj);
}

class MScStudentBizz:StudentBizz
{
    public override Boolean enrollNewStudent(MScStudent stuObj)
    {
        //code
    }
}

2 个答案:

答案 0 :(得分:3)

根据代码的其余部分,另一种方法可能是使用generics with type constraints(此处我假设MScStudent继承自Student):

abstract class StudentBizz<TStudent> where TStudent : Student
{
    public abstract Boolean enrollNewStudent(TStudent stuObj);
}

class MScStudentBizz:StudentBizz<MScStudent>
{
    public override Boolean enrollNewStudent(MScStudent stuObj)
    {
        //code
    }
}

答案 1 :(得分:1)

重写方法应与基类方法具有相同的签名。 因此,您的重写方法应如下所示:

class MScStudentBizz: StudentBizz 
{ 
  public override Boolean enrollNewStudent(Student student) 
  { 
     //code
  } 
}
相关问题