-2

我有一门课,里面有几本书,并在控制台屏幕上打印出来,这是我的代码:

class Book
{
    public string forfattareEfternamn;
    public string forfattareFornamn;
    public string bokensTittle;
    public int lanseringsDatum;

    public Book(string forfattareEfternamn, string forfattareFornamn, string bokensTittle, int lanseringsDatum)
    {


    }   


    public string BokensTittle
    {
        get { return bokensTittle; }
        set { bokensTittle = value; }
    }
    public string ForfattareFornamn
    {
        get {return forfattareFornamn;}
        set {forfattareFornamn = value;}
    }

    public string ForfattareEfternamn
    {
        get {return forfattareEfternamn;}
        set {forfattareEfternamn = value;;}
    }

    public int LanseringsDatum
    {
        get { return lanseringsDatum; }
        set { lanseringsDatum = value; }
    }

    public override string ToString()
    {
        return string.Format("{0}, {1}, {2}, {3} ", forfattareEfternamn, ForfattareFornamn, bokensTittle, lanseringsDatum);

    }
}

主要的:

class Program
{
    static void Main(string[] args)
    {
        List<Book> books = new List<Book>(string forfatareFornamn, string forfattareEfternamn, string bokensTittle, int lanseringsDatum);
        books.Add(new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Count Of Monte Cristo", lanseringsDatum = 1844 });
        books.Add(new Book { forfattareFornamn = "Clark", forfattareEfternamn = "Arthur C", bokensTittle = "Rendezvous with Rama", lanseringsDatum = 1972 });
        books.Add(new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Three Musketeers", lanseringsDatum = 1844 });
        books.Add(new Book { forfattareFornamn = "Defoe", forfattareEfternamn = "Daniel", bokensTittle = "Robinson Cruise", lanseringsDatum = 1719 });
        books.Add(new Book { forfattareFornamn = "Clark", forfattareEfternamn = "Arthur C", bokensTittle = "2001: A space Odyssey", lanseringsDatum = 1968 });

        foreach (Book b in books)
        {
            Console.WriteLine(b);
        }
        Console.ReadKey();
    }
}

现在,问题是我被告知要使用包含数据类型的构造函数,所以我不必一一写下所有书名,我实际上不知道我应该怎么做。我试过了:

public Book(string forfattareEfternamn, string forfattareFornamn, string bokensTittle, int lanseringsDatum)
    {


    }   

但它给了我一个错误,说我没有接受 0 个参数的构造函数。有任何想法吗?

4

3 回答 3

0

如果这是您的构造函数:

public Book(string forfattareEfternamn, string forfattareFornamn, string bokensTittle, int lanseringsDatum)
{
} 

此行不会编译:

new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Count Of Monte Cristo", lanseringsDatum = 1844 })

如果您创建一个,parametrized constructor那么您将不再拥有一个default constructor. 因此,您遇到了此编译器错误。

你可以做两件事:

1)如果你想编译这个,在你的Book类中添加一个无参数的构造函数。

public Book() {} 

2)或更改您的主类中的代码,使其与您的参数化构造函数相对应new Book("blah", "blah", etc);

于 2013-02-25T08:10:56.280 回答
0

首先你的List初始化是错误的:

List<Book> books = new List<Book>(string forfatareFornamn, string forfattareEfternamn, string bokensTittle, int lanseringsDatum);

将其更改为:

List<Book> books = new List<Book>();

然后在调用构造函数后,你不为字段赋值,这样做:

public Book(string forfattareEfternamn, string forfattareFornamn, string bokensTittle, int lanseringsDatum) 
{
   this.forfattareEfternamn = forfattareEfternamn;
   this.forfattareFornamn = forfattareFornamn;
   this.bokensTittle =bokensTittle;
   this.lanseringsDatum = lanseringsDatum;
} 

最后一个错误是您的逻辑调用Book构造函数:

(new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Count Of Monte Cristo", lanseringsDatum = 1844 });

将其更改为:

books.Add((new Book ("Dumas", "Alexandre", "The Count Of Monte Cristo", 1844));
于 2013-02-25T08:11:24.597 回答
0

new Book { ... }是隐式new Book() { ... }的 - 即它使用默认构造函数。如果添加自定义构造函数,则可能还需要添加显式无参数构造函数:

public Book() {}

这里的问题是,如果您没有定义任何构造函数(在非抽象类上),它会添加一个隐式的公共无参数构造函数,该构造函数会调用: base()- 但是,如果您添加任何自定义构造函数,它不会这样做

或者 - 更改其他代码,即

books.Add(new Book("Dumas", "Alexandre", "The Count Of Monte Cristo", 1844 ));
于 2013-02-25T08:11:24.327 回答