C#打印字符串

本文关键字:字符串 打印 | 更新日期: 2023-09-27 18:27:02

我已经向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;
        }
    }
   }
}

C#打印字符串

您可能想要

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

我已经重写了代码,使其更加清晰,看看吧,这应该可以让你轻松地指定作者的年龄,并将其打印回来:

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);
        }
    }
}