【问题标题】:C# lower the price with get/set if the value is over x如果值超过 x,C# 使用 get/set 降低价格
【发布时间】:2016-07-06 19:09:25
【问题描述】:

如果价格超过 30,我需要使用 get 和 set 命令将价格降低 10%。价格没有变化,显然有些东西我没有注意到。该任务需要使用属性。 Get 必须返回 price 命令的值。如果在 set 命令中使用了一个提示。

using System;
class Book
{
public string Name;
public string Writer;
public string publisher;
private float price;
public string genre;



public Book(string Name, string Writer, string publisher, float price, string genre)
{
    this.Name = Name;
    this.Writer = Writer;
    this.publisher = publisher;
    this.price = price;
    this.genre = genre;
}    

public float Price
{
    get
    {
        return price;
    }
    set
    {
        if (value > 30)
        {
            price = value *0.90f;                
        }
        else
        {
            price = value;
        }
    }
}
static void Main(string[] args)
{
    Book b = new Book("First Book", "W. Writer", "publish company 1", 21.50f, "Crime");
    Book b2 = new Book("Second Book", "T. Typer", "publish company 2", 36.90f, "Fantasy");
    Console.WriteLine(b.price);
    Console.WriteLine(b2.Price);
}}

【问题讨论】:

    标签: c# properties get set


    【解决方案1】:

    问题出在您的构造函数中。您无需通过房产访问价格,而是直接访问它。你的类需要一个小的重构。

    class Book
    {
        public string Name { get; set; }
        public string Writer { get; set; }
        public string Publisher { get; set; }
        public string Genre { get; set; }
    
        private float _price;
        public float Price
        {
            get
            {
                return _price;
            }
            set
            {
                if (value > 30)
                {
                    _price = value * 0.90f;
                }
                else
                {
                    _price = value;
                }
            }
        }
    
        public Book(string name, string writer, string publisher, float price, string genre)
        {
            Name = name;
            Writer = writer;
            Publisher = publisher;
            Price = price;
            Genre = genre;
        }
    }
    

    最好不要直接暴露类的字段。您可以通过使用属性来做到这一点,这促进了封装的概念。以下属性有什么区别:

    public string Name { get; set; }
    

    以及名为Price 的属性。第一个是自动实现的属性。这意味着,在编译代码时,将创建 Name 的支持字段,并在运行时返回该字段的值,当您访问它时(book.Name,其中book 是 @ 的一个实例987654326@)。相当于如下:

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

    此外,请记住我们上面使用的命名约定,因为这对许多 C# 代码库很常见。属性以大写字母开头,而私有字段以小写字母开头(始终使用驼峰式大小写),有时前缀为下划线 _。

    【讨论】:

      【解决方案2】:

      当然它从来没有设置,因为你从来没有设置一个新的价格,你的构造函数只设置了价格标签,而不是属性。

      如果你加b2.Price = 36.90f;

      在您的 Console.Writeline 之前,它将设置正确的值。

      如果要在构造函数中删除 10%,则替换该行

      this.price = price;
      

      与

      this.Price = price;
      

      另外我建议你应该稍微重构一下你的代码。 :)

      【讨论】:

        【解决方案3】:

        在 main 函数中,您使用构造函数实例化 Book 对象,在构造函数中,您设置价格字段,而不是属性。因此,您为设置价格字段而编写的代码永远不会被执行。您可以更改构造函数以设置属性而不是字段(即 Price 大写 P )

        this.Price = price;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-24
          • 1970-01-01
          • 2012-11-07
          • 2017-11-15
          • 1970-01-01
          • 2022-06-27
          • 2013-04-12
          • 2022-07-06
          相关资源
          最近更新 更多