C#打印字符串

时间:2014-10-18 14:05:38

标签: c#

我已经为Book添加了一个公共的GetSummary方法,我希望返回一个包含标题,作者和作者年龄的字符串。但是,我坚持年龄部分,我知道我必须添加一些人的年龄,但似乎不能这样做

任何帮助都将受到高度赞赏

由于

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {
        Book[] books = new Book[4];  //declare an array of Book

        books[0] = new Book("Moby Dick");
        books[0].author = new Person("Herman Melville");
        books[1] = new Horror("The Creeping");
        for (int i = 0; i < 3; i++)
            Console.WriteLine(books[i].GetSummary);
        Console.ReadKey();
     }
   }
}

class Person : IComparable
{
    private int age;
    private string name;

    //constructor with one argument        
    public Person(string name)
    {
        this.name = name;
        age = 18; //default age
    }

    //property for name            
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public int CompareTo(Object obj) //implementation of CompareTo
    {                   // for IComparable

        Person other = (Person)obj;
        return Name.CompareTo(other.Name); //uses Name for comparison        
    }

}
}

class Horror : Book
{
    public Horror(string title): base(title)
    {
        base.author = new Person("Stephen King");
    }
  }

}

class Book
{
    public string title;
    public Person author;

    public Book(string title)
    {
        author = new Person(" ");
        this.title = title;
    }

    public string AuthorName
    {
        get { return author.Name; }
        set { author.Name = value; }
    }

    public string GetSummary
    {
        get
        {
            string x = title + ", " + AuthorName;
            return x;
        }
    }
   }
}

2 个答案:

答案 0 :(得分:2)

你可能想要

public string GetSummary
    {
        get
        {
            return string.Format("{0}, {1}, {2}", title, author.Name, author.Age);
        }
    }

答案 1 :(得分:0)

我已经重新编写了更清晰的代码,请看一下,这应该可以让您轻松指定作者的年龄并将其打印回来:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Change array to a list, this allows me to add books to my collection without specifing indexes
            //See http://www.dotnetperls.com/list
            List<Book> books = new List<Book>();

            //I've changed the way you add books, now you declare a new book, and within it you declare a new person which is now a author of that new book
            books.Add(new Book("Moby Dick", new Person("Herman Melville", 72)));
            books.Add(new Book("The Creeping", new Person("Alexandra Sirowy", null)));

            //Use a foreach loop to iterate through the list printing the summary to each
            foreach (var book in books)
            {
                Console.WriteLine(book.Summary);
            }

            //Exit
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }
}

class Person : IComparable
{
    //Some syntax change here, people may disagree on my variable names
    private int? _age;
    private string _name;

    //Changed default constructor to accept age but made the age nullable(if you don't know the age of the person you can pass in null)
    //http://www.dotnetperls.com/nullable-int
    public Person(string name, int? age)
    {
        Name = name;
        Age = age;
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int? Age
    {
        get { return _age; }
        set { _age = value; }
    }

    //Hmmm don't know about this are you sure you want to use name as the primary key, 
    //what if you have two people with the same names but different ages, are they the same person?
    public int CompareTo(Object obj)
    {
        return Name.CompareTo(((Person)obj).Name);
    }
}

class Book
{
    //Made these auto properties
    public string Title { get; set; }
    public Person Author { get; set; }

    //Now the book default constructor accepts a author, this forces you(when using this function to create a new book)
    //To specifiy a person as a author
    public Book(string title, Person author)
    {
        Author = author;
        Title = title;
    }

    //Used "Darren Young" code, this is much better
    public string Summary
    {
        get
        {
            return string.Format("{0}, {1}, {2}", Title, Author.Name, Author.Age);
        }
    }
}