【发布时间】: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