在c#

时间:2018-06-30 15:36:44

标签: c#

我已经在名为“图书管理员”的课程中创建了一个书单,如果我 尝试从同一个班级打印书目表就可以了。 但是,当我尝试在另一堂课上做同样的事情时,它将无法工作 完全没有 谁能看到我犯了什么错误,也许可以帮助我解决问题?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Librarian obj = new Librarian();
            obj.AddToBooklist();
            Console.ReadKey();
        }
    }
    class Book
    {
        public string author;
        public String title;
    }
    class Librarian
    {
        public List<Book> booklist = new List<Book>();
        public void AddToBooklist()
        {
            //create a book to add into booklist
            Novel newBook = new Novel();
            newBook.author = "Henri";
            newBook.title = "Papillon";
            booklist.Add(newBook);

            foreach (var item in booklist)
            {
                Console.WriteLine(item.author + " " + item.title);// Prints fine
            }
            Console.WriteLine(booklist[0].author + " " + booklist[0].title);// prints fine too
            // create an object to get into Novel class
            Novel objNovel = new Novel();
            objNovel.Print();
        }
    }
    class Novel : Book
    {
        public void Print()
        {
            Librarian objLib = new Librarian();// create object to get into Librarian class

            foreach (var item in objLib.booklist)
            {
                Console.WriteLine(item.author + " " + item.title);// prints nothing
            }
            Console.WriteLine(objLib.booklist[0].author); // causes program to crash
        }
    }
}

1 个答案:

答案 0 :(得分:1)

看看您的代码,类型Novel的实例没有任何书籍添加到书籍列表中。

将您的代码更改为以下内容:

class Program
{
    static void Main(string[] args)
    {
        Librarian obj = new Librarian();
        obj.AddToBooklist();

        //this needs to be moved here, instead to be in the add to book list method.
        Novel objNovel = new Novel();
        objNovel.Print();

        Console.ReadKey(); 
    }
}

class Book
{
    public string author;
    public String title;
}

class Librarian
{
    public List<Book> booklist = new List<Book>();
    public void AddToBooklist()
    {
        //create a book to add into booklist
        Novel newBook = new Novel();
        newBook.author = "Henri";
        newBook.title = "Papillon";
        booklist.Add(newBook);

        foreach (var item in booklist)
        {
            Console.WriteLine(item.author + " " + item.title);// Prints fine
        }
        Console.WriteLine(booklist[0].author + " " + booklist[0].title);// prints fine too
    }                                                             // create an object to get into Novel class 
}

class Novel : Book
{
    public void Print()
    {
        Librarian objLib = new Librarian();// create object to get into Librarian class

        objLib.AddToBooklist();//invoke the method that will add the book to the list

        foreach (var item in objLib.booklist)
        {
            Console.WriteLine(item.author + " " + item.title);// prints nothing
        }
        Console.WriteLine(objLib.booklist[0].author); // causes program to crash
    }
}

更新#1。

回答第一个问题,导致无休止的循环问题。我拿走了您的代码并对其进行了一些更改。我写的最初修正仍然存在。另外,我移动了这段代码:

  Novel objNovel = new Novel();
  objNovel.Print();

改为main方法,而不是它的原始位置(请参阅问题)。

希望这会有所帮助, 欢呼和快乐的编码!