我编写的程序可以正常工作,并且打印效果很好。它可以很好地创建两个对象。一个对象是使用无参数默认构造函数创建的,另一个是从非默认构造函数创建的。唯一的区别是我应该为 Author 使用 set 关键字来创建默认值。因此,当我使用错误的作者姓名创建对象时,它将使用 set 关键字对其进行更改。
当我为非默认构造函数输入错误的 Book1.Author 值时,它会更改两个对象中的两个作者名称。我如何只允许它更改我的 Book1 对象中的作者姓名?
using System;
namespace Book
{
public class Book
{
private string _Author;
private string _Title;
private string _Keywords;
private string _publicationDate;
private string _ISBN;
public Book()
{
Author = "";
}
public Book(string title, string author, string publicationDate, string keywords, string isbn)
{
Title = title;
Author = author;
Keywords = keywords;
PublicationDate = publicationDate;
ISBN = isbn;
}
public string Title { get => _Title; set => _Title = value; }
public string Author { get => _Author; set => _Author = "Mary Delamater and Joel Murach, "; }
public string Keywords { get => _Keywords; set => _Keywords = value; }
public string PublicationDate { get => _publicationDate; set => _publicationDate = value; }
public string ISBN { get => _ISBN; set => _ISBN = value; }
public override string ToString()
{
return Title + Author + PublicationDate + "Keywords: " + Keywords + "ISBN " + ISBN;
}
}
}
using System;
namespace Book
{
class Program
{
static void Main(string[] args)
{
Book Book1 = new Book("murach's ASP.NET Core MVC, ", "Mary Delamater and Joel Murach, ", "January 2020, ", "C#, Programming, MVC, ASP.NET, Core, Beginner", "978-1-943872-49-7");
Console.WriteLine(Book1.ToString());
Book Book2 = new Book();
Book2.Title = "C# In Depth, ";
Book2.Author = "John Skeet, ";
Book2.PublicationDate = "March 23, 2019, ";
Book2.Keywords = "C#, InDepth";
Book2.ISBN = "9781617294532";
Console.WriteLine(Book2.ToString());
}
}
}