2014-10-18 202 views
0

我已经添加了一个公共的GetSummary方法来预订我想返回一个包含标题,作者和作者年龄的字符串。然而,IM卡上了年纪的一部分,我知道我必须添加一些人的年龄,但不能似乎做C#打印字符串

任何帮助,将不胜感激

感谢

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; 
     } 
    } 
    } 
} 
+0

你到底在干什么? – 2014-10-18 14:07:28

+0

这不是你的问题,但除非你想不断更新数据,否则你应该删除Age的setter,添加BirthDate属性并从出生日期计算年龄 – 2014-10-18 14:13:22

+0

删除实例变量并使[Auto-Implemented Properties](http ://msdn.microsoft.com/en-us/library/bb384054.aspx)。你也不需要有AuthorName。 – NMK 2014-10-18 14:13:45

回答

2

你可能想

public string GetSummary 
    { 
     get 
     { 
      return string.Format("{0}, {1}, {2}", title, author.Name, author.Age); 
     } 
    } 
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); 
     } 
    } 
} 
+0

谢谢大家的意见,现在我明白了我错了:D – 2014-10-18 15:03:00