【问题标题】:Using set to change property value in a constructor, C#使用 set 在构造函数中更改属性值,C#
【发布时间】:2021-10-24 01:56:23
【问题描述】:

我编写的程序工作正常,打印效果很好。它可以很好地创建两个对象。一个对象是使用无参数默认构造函数创建的,另一个是从非默认构造函数创建的。唯一的区别是我应该为 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());

        }
    }
}

【问题讨论】:

  • 你能把样本贴出来吗?
  • “当我为 Book1.Author 输入错误的值时……它会更改两个对象中的两个作者姓名。”你在说什么对象?
  • set => _Author = "Mary Delamater and Joel Murach, "; 应该是set => _Author = value;,确定吗?你会分配预期的字符串从特定的地方? setter 不是默认值。这就是任何时候你为属性分配任何值时发生的事情,而现在,你会忽略被分配的值,并将字段更改为固定值

标签: c# constructor object-create


【解决方案1】:

你对程序流程的理解是错误的。在您的构造函数中,当您调用属性来设置值时,程序流将运行您硬编码为字符串的set 部分。现在,如果您尝试通过对象或构造函数中的此属性保存您的值,无论您想要什么,它都会始终设置为硬编码值。要存储自定义值,您必须使用 set=>value 并应用您的业务约束,您可以在属性中编写逻辑条件,这就是使用属性来实现封装的原因。

类似这样的:

public string Author{
  get{
    return _Author;
  }
  set{
    if()//your condition to validate if author is wrong
      _Author = ""; //your expected correct author name
    else
      _Author = value
  }
}

【讨论】:

    猜你喜欢
    • 2013-08-01
    • 2022-12-16
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多