我正在开发一个包含不同书籍的通用列表的程序。我遇到的问题是,我的书本类应该覆盖ToString()
超类中的方法,System.Object
以便它显示如下字符串:
作者名字,作者姓氏,“书名”,年份。
这是我的书类代码:
class Book
{
public string bookTitle
{
get;
set;
}
public string authorFirstName
{
get;
set;
}
public string authorLastName
{
get;
set;
}
public int publicationYear
{
get;
set;
}
}
这是我的代码Main
:
static void Main(string[] args)
{
List<Book> books = new List<Book>();
books.Add(new Book { authorFirstName = "Dumas", authorLastName = "Alexandre", bookTitle = "The Count Of Monte Cristo", publicationYear = 1844 });
books.Add(new Book { authorFirstName = "Clark", authorLastName = "Arthur C", bookTitle = "Rendezvous with Rama", publicationYear = 1972 });
books.Add(new Book { authorFirstName = "Dumas", authorLastName = "Alexandre", bookTitle = "The Three Musketeers", publicationYear = 1844 });
books.Add(new Book { authorFirstName = "Defoe", authorLastName = "Daniel", bookTitle = "Robinson Cruise", publicationYear = 1719 });
books.Add(new Book { authorFirstName = "Clark", authorLastName = "Arthur C", bookTitle = "2001: A space Odyssey", publicationYear = 1968 });
}
因此,关于我应该如何处理“覆盖ToString()
超类中的方法System.Object
,使其返回具有以下格式的字符串”的任何想法:
作者名字,作者姓氏,“书名”,年份。