c# - C# 访问属性中的属性?
问题描述
我正在尝试通过使用属性的 Exercism C# 练习“称重机”中的一些测试。我有这些属性:
using System;
enum Units
{
Pounds,
Kilograms
}
class WeighingMachine
{
private decimal inputWeight;
public decimal InputWeight
{
get { return inputWeight; }
set { if (value >= 0)
inputWeight = value;
else throw new ArgumentOutOfRangeException();
}
}
private decimal displayWeight;
public decimal DisplayWeight
{
get
{ return displayWeight; }
set
{
displayWeight = InputWeight - TareAdjustment;
if (displayWeight <= 0)
throw new ArgumentOutOfRangeException();
}
}
private decimal _pounds;
public USWeight USDisplayWeight
{
get { return _pounds; }
set { _pounds = new USWeight(InputWeight).Pounds; }
}
public decimal TareAdjustment { private get; set; }
public int Units
{ get; set; }
}
struct USWeight
{
public USWeight(decimal weightInPounds)
{
Pounds = (int)weightInPounds;
Ounces = (weightInPounds - Pounds)*16;
}
public int Pounds { get; set; }
public decimal Ounces { get; set; }
}
我的绊脚石是测试:
[Fact]
public void Get_us_display_weight_pounds()
{
var wm = new WeighingMachine();
wm.InputWeight = 60m;
Assert.Equal(132, wm.USDisplayWeight.Pounds);
}
我无法理解测试如何要求 wm.USDisplayWeight.Pounds - 最后如何访问 .Pounds?就像在 USDisplayWeight 属性中设置了一个属性,但这不可能是吗?我不能让编译器停止抱怨 .Pounds - 我得到“十进制”不包含“磅”的定义,并且找不到接受“十进制”类型的第一个参数的可访问扩展方法“磅”。
我敢肯定这是我在这里忽略的一件简单的事情,但我会很感激任何帮助。
解决方案
推荐阅读
- python - 如何获取列表中str和int元素组合的最小值
- ios - Xcode 12.1 中的 errSecInternalComponent 错误
- c# - 如何在统一动作闭包中访问游戏对象
- python-3.x - Flask-migrate 不能识别包中的所有模块?
- rest - 如何解决 System.CalloutException: Unauthorized endpoint,请检查 Setup->Security->Remote site settings for Google docs API
- javascript - this.form.submit() 之后未显示选定的下拉值
- python - 如何根据 scikit learn 的较新版本修改该行?
- python - Rasterio MemoryFile 到 Pillow 图像(或 base64 输出)
- typescript - 将类注入自定义存储库
- bert-language-model - “您必须指定 input_ids 或 inputs_embeds”,但我确实指定了 input_ids