我已经进入 OOP 的第二周了,我们的导师给出了一个消息传递练习,我遇到了一个我似乎无法解决的错误。错误是:
Obj_Orientated_CD_MOVIE_Library.Movie' does not implement inherited abstract member 'Obj_Orientated_CD_MOVIE_Library.borrowCopy()'
这又导致了随后的错误:
Obj_Orientated_CD_MOVIE_Library.Movie.borrowCopy(Obj_Orientated_CD_MOVIE_Library.Student)': no suitable method found to override
我不确定我是否解释正确,这是代码。
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");
}
}
}
请提出建设性的批评和帮助。
请您参考如下方法:
您似乎错误地覆盖了 borrowCopy。我们需要查看 Item 的代码,但您提供的签名:
void borrowCopy(Student objStudent)
...与Item中抽象方法的签名不一样
从错误看来,Item 中的方法没有任何参数。
所以您遇到了两个错误,首先您没有提供具有所需签名的 borrowCopy 的实现,其次您对 Item 上不存在的签名使用了 override 关键字。
试试这个:
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");
}
}
