我在OOP的第二周,我们的导师已经做了一个消息传递练习,我有一个我似乎无法解决的错误。错误是:
Obj_Orientated_CD_MOVIE_Library.Movie'未实现继承的抽象成员'Obj_Orientated_CD_MOVIE_Library.borrowCopy()'
反过来导致了后续错误:
Obj_Orientated_CD_MOVIE_Library.Movie.borrowCopy(Obj_Orientated_CD_MOVIE_Library.Student)':找不到合适的方法来覆盖
我不确定我是否已正确解释,这是代码。
abstract class Item
{
protected int copies;
protected int availCopies;
protected string title;
public Item(string newTitle, int newCopies, int newAvailCopies)
{
title = newTitle;
copies = newCopies;
availCopies = newAvailCopies;
}
public abstract void borrowCopy(Student objStudent);
public abstract void returnCopy();
public int Copies
{
get
{
return copies;
}
set
{
copies = value;
}
}
public int AvailCopies
{
get
{
return availCopies;
}
set
{
availCopies = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
}
class Student
{
public string date;
public string returnDate;
public Student(string newDate, string newReturnDate, string newTitle, int newCopies, int newAvailCopies)
: base(newTitle, newCopies, newAvailCopies)
{
date = newDate;
returnDate = newReturnDate;
}
public void borrowMovie(Movie objMovie)
{
objMovie.borrowCopy(this);
}
public void borrowCD(CD objCD)
{
objCD.borrowCopy(this);
}
public override void borrowCopy(Student objStudent)
{
}
public override void returnCopy()
{
}
}
class Movie : Item
{
public string runTime;
public Movie(string newTitle, string newRunTime, int newCopies, int newAvailCopies)
: base(newTitle, newCopies, newAvailCopies)
{
runTime = newRunTime;
}
public string Runtime
{
get
{
return runTime;
}
set
{
runTime = value;
}
}
public override void returnCopy()
{
availCopies++;
Console.Write("\nThank you for returning the Movie\n");
}
public override void borrowCopy(Student objStudent)
{
if (AvailCopies > 0)
{
AvailCopies--;
Console.Write("Please refer to receipt for return date of the Movie\n");
}
else
{
Console.WriteLine("\nThis Movie is not available at this time, please try again later");
}
}
}
class CD : Item
{
public string band;
public CD(string newTitle, string newBand, int newCopies, int newAvailCopies)
: base(newTitle, newCopies, newAvailCopies)
{
band = newBand;
}
public string Band
{
get
{
return band;
}
set
{
band = value;
}
}
public override void returnCopy()
{
availCopies++;
Console.Write("\nThank you for returning the CD\n");
}
public override void borrowCopy(Student objStudent)
{
if (AvailCopies > 0)
{
AvailCopies--;
Console.Write("Please refer to receipt for return date of the CD\n");
}
else
{
Console.WriteLine("\nThis CD is not available at this time, please try again later");
}
}
}
建设性的批评和帮助请。
答案 0 :(得分:3)
您似乎错误地覆盖了borrowCopy。我们需要查看Item的代码,但是您要提供的签名:
void borrowCopy(Student objStudent)
...与Item中抽象方法的签名不同。
从错误中看,Item中的方法没有任何参数。
所以你得到了两个错误,首先你没有提供带有所需签名的borrowCopy实现,其次你使用override关键字作为Item上不存在的签名。
试试这个:
public override void borrowCopy()
{
if (AvailCopies > 0)
{
AvailCopies--;
Console.Write("Please refer to receipt for return date of the Movie\n");
}
else
{
Console.WriteLine("\nThis Movie is not available at this time, please try again later");
}
}
答案 1 :(得分:1)
你问了详细资料,你走了。这是你需要改变的东西。更新的Student
类:
//is a Student an Item? I think not
class Student
{
public string date;
public string returnDate;
public Student(string newDate, string newReturnDate)
{
date = newDate;
returnDate = newReturnDate;
}
public void borrowMovie(Movie objMovie)
{
objMovie.borrowCopy(this);
}
public void borrowCD(CD objCD)
{
objCD.borrowCopy(this);
}
}
注意我在这里删除了Item
抽象类的继承,Student
不是Item
(我挑战你提出你可以借用的情况“学生的副本,假设我们在谈论的是人类学生”。我可以看到你想要一些其他的类字段,比如一个名字值(我稍后会回到这里)。
然后Item
更新了两个方法签名:
public abstract void borrowCopy(Student objStudent);
public abstract void returnCopy(Student objStudent);
然后,当您从movie.borrowCopy(this);
内拨打Student
时,您不会收到任何错误,并且可以使用该方法中Student
中的任何方法或属性。如果您在Name
中添加了Student
属性,则可以修改Console.Writeline
中的borrowCopy()
以说“Joe Blow,请参阅电影的返回日期的收据”像这样更新它:Console.Write("{0}, please refer to receipt for return date of the Movie\n", student.Name);