我的假设情况:
在新世界秩序中,单一政府要求所有银行向其客户提供相同的利率。因此,所有银行都应同意利率。另外,银行需要制定政策 可以改变(增加或减少)兴趣的地方 或不。如果(至少)一家银行不愿意改变利率,则在成功进行谈判之前,不得修改利率。
我的C#程序如下所示
namespace NewWorldOrder
{
public class Bank
{
public static float rate;
private bool allow_rate_modification;
private string bankname;
// Property
public string Bankname
{
// Assume some Business Logic is added to filter values
// Omitting those for brevity
get => bankname;
set => bankname = value;
}
// Property
public bool AllowRateModification
{
// Assume some Business Logic is added to filter values
// Omitting those for brevity
get => allow_rate_modification;
set => allow_rate_modification = value;
}
static Bank()
// To set the static field at runtime
{
// In actual case, this value may be initialized from a db
// Again doesn't matter how it is initialized.
rate = 4.5f;
}
// The ctor
public Bank(string bank_name_, bool allow_modification)
{
Bankname = bank_name_;
AllowRateModification = allow_modification;
}
}
class Program
{
static void Main(string[] args)
{
Bank rbs =new Bank("Royal Bank of Scotland",true);
Bank lloyds = new Bank("LLoyds", true);
Bank hsbc = new Bank("HSBC", false);
Bank.rate = 4.7f; // This should not happen as HSBC is not open to rate change
// Irrelevant Stuff
// ...
// ...
}
}
}
长话短说:
如何将静态(类)属性绑定到所有实例中的一个特定(通常为布尔值)实例属性。还是有另一种合乎逻辑的方法 在C#中可以做到这一点?
注意:我是C#的新手,如果这完全是错误的话,请原谅我。
答案 0 :(得分:2)
听起来像您需要一个列表, linq 可以帮助您进行查询
var list = new List<Bank>()
{
new Bank("Royal Bank of Scotland", true),
new Bank("LLoyds", true),
new Bank("HSBC", false)
};
if (list.All(x => x.AllowRateModification))
{
// all banks Allow Rate Modification
}
您可以使用课程来管理银行
public class Exchange
{
public List<Bank> Banks { get; set; } = new List<Bank>();
public void NegotiateRates()
{
while (!CanModifyRates)
{
// to the rate stuff in here
}
}
public bool CanModifyRates => Banks.All(x => x.AllowRateModification);
}
...
private static void Main()
{
var exchange = new Exchange();
exchange.Banks.Add(new Bank("Royal Bank of Scotland", true));
exchange.Banks.Add(new Bank("LLoyds", true));
exchange.Banks.Add(new Bank("HSBC", false));
exchange.NegotiateRates();
}
其他资源
表示可以由以下对象访问的对象的强类型列表 指数。提供搜索,排序和操作列表的方法。
Enumerable.All(IEnumerable, Func) Method
确定序列中的所有元素是否都满足条件。