我使用set / get函数是否错误?

fib*_*ot. 6 c# class

我需要在程序中解决与get / set函数有关的问题。

这是完整的未编辑代码:https://pastebin.com/Vd8zC51m

编辑:我的好朋友找到了解决方案,就是这样使用“值”:

        {
            get
            {
                return this.returnValue;
            }

            set
            {
                if (value > 30)
                {
                    this.returnValue = value - (value * 0.10f);
                } else
                {
                    this.returnValue = value;
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

很抱歉,如果我浪费了任何时间...!

问题是这样的:创建一个Price属性,如果使用set / get超过30,价格属性将价格降低10%。

我得到的是:值为0。

I know what's wrong, I just don't know how to fix it. I know it's in the set command. After I show the code, you might have a better understanding.

I've tried searching around the internet how to use the set command correctly and tried different ways of doing the set command.

public class Book
{
    public string Name;
    public string writerName;
    public string bPublisher;
    public float bPrice;
    public string bTheme;
    public float returnValue;

    public Book(string name, string writer, string publisher, float price, string theme)
    {
        Name = name;
        writerName = writer;
        bPublisher = publisher;
        bPrice = price;
        bTheme = theme;
    }

    public float Price
    {
        get
        {
            return returnValue;
        }

        set
        {
            if (this.bPrice > 30)
            {
                returnValue = this.bPrice - (this.bPrice * 0.10f);
            } else
            {
                returnValue = this.bPrice;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

----------These are the main parts cut from the program----------------------

static void Main(string[] args)
{
    Book k2 = new Book("A book", "O. Writer", "Publisher Ab", 36.90f, "Fantasy");
    Console.WriteLine(k2.Price);
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 7

So we have two prices here: net (e.g. 45.00) and reduced price (45.00 - 4.50 == 41.50)

public Book {
  ...
  const Decimal PriceThreshold = 30.0m;
  const Decimal ReducePerCent = 10.0m; 

  private Decimal m_NetPrice;

  // Net price
  // Decimal (not Single, Double) usually is a better choice for finance
  public Decimal NetPrice {
    get {
      return m_NetPrice;
    }
    set {
      if (value < 0) 
        throw new ArgumentOutOfRangeException(nameof(value));

      m_NetPrice = value;
    }
  }  

  // Price with possible reduction
  public Decimal Price {
    get {
      return NetPrice > PriceThreshold 
        ? NetPrice - NetPrice / 100.0m * ReducePerCent
        : NetPrice;
    } 
  } 
}
Run Code Online (Sandbox Code Playgroud)

Please, note that we don't have set for Price property; there's ambiguity since one Price, say, 28.80 corresponds to two valid NetPrices (28.80 or 32.00: 32.00 - 3.20 == 28.80)


Mon*_*Zhu 6

Why don't you put the logic into the getter. It seems to make more sense since you don't use value in the setter:

public float Price
{
    get
    {
        if (this.bPrice > 30)
        {
            return this.bPrice - (this.bPrice * 0.10f);
        } 
        else
        {
            return this.bPrice;
        }
    }

    private set
    {
        this.bPrice = value
    }
}
Run Code Online (Sandbox Code Playgroud)

EDIT:

a short version of the getter would look like this and (thanks to Patrick Hofman) you can calculate the 90% by multiplication with 0.9:

return this.bPrice > 30? this.bPrice * 0.90f : this.bPrice;


public float Price
{
    get { return this.bPrice > 30? this.bPrice * 0.90f : this.bPrice; }

    private set { this.bPrice = value; }        
}
Run Code Online (Sandbox Code Playgroud)

I made the setter private. Remove it if you want to allow the setting/manipulation of the price also after the creation of your Book object.

  • 返回this.bPrice * 0.90f; (3认同)
  • 这(在帕特里克·霍夫曼(Patrick Hofman)的建议下),当我读到“ *如果…………将使**价格降低10%”,我会自动认为“吸气”。 (2认同)