C# Static 属性 in non-static class as indicator of main object of this class
C# Static property in non-static class as indicator of main object of this class
我已经创建了 class 个重要对象和此 class 的一些对象。我想让用户选择这个 class 的主要对象。看看下面的代码:
public class Program
{
public static void Main(string[] args)
{
Important imp1 = new Important("Important 1");
Important imp2 = new Important("Important 2");
Important imp3 = new Important("Important 3");
imp2.SetMostImportant();
Console.Write(Important.MostImportant.Name);
}
public class Important
{
public Important(string name)
{
Name = name;
if(MostImportant == null)
SetMostImportant();
}
public string Name { get; private set; }
public static Important MostImportant { get; private set; }
public void SetMostImportant()
{
MostImportant = this;
}
}
}
这是好的解决方案吗?如果不是,请告诉我为什么。
之前,为了实现这种事情,我只是创建了一个布尔字段,例如IsMainObject 并且,当我想更改主要对象时,我遍历了特定 class 的所有对象(或对象组)除了我想成为主要的元素,并将布尔值更改为 false,在我的新候选人中我只是将标志设置为真。示例如下:
public class Program
{
public static void Main(string[] args)
{
Important imp1 = new Important("Important 1");
Important imp2 = new Important("Important 2");
Important imp3 = new Important("Important 3");
List<Important> list = new List<Important> { imp1, imp2, imp3 };
foreach(var item in list.Where(x => x.Name != "Important 2"))
{
item.SetMostImportant(false);
}
imp2.SetMostImportant(true);
Console.Write(list.FirstOrDefault(x => x.MostImportant == true).Name);
}
public class Important
{
public Important(string name)
{
Name = name;
}
public string Name { get; private set; }
public bool MostImportant { get; private set; }
public void SetMostImportant(bool val)
{
MostImportant = val;
}
}
}
我不喜欢这个解决方案,因为:
- 我不知道
MostImportant
是否适用于多个对象而不进行迭代。
- 我需要编写额外的代码来处理更多的案例。
- 我不可能总是遍历特定 class 的所有实例(组并不总是足够)。
...还有更多,但您明白了。
public static Important MostImportant { get; private set; }
是一个很好的解决方案,比
好得多
public bool MostImportant { get; private set; }
在实现 "singleton" 类 时,拥有静态类型 属性 的情况并不少见。我写了类似这样的代码:
class MyClass
{
public static MyClass Instance { get; private set; }
public MyClass()
{
if (Instance == null)
{
Instance = this;
}
else
{
throw new Exception("MyClass already instantiated.");
}
}
}
我已经创建了 class 个重要对象和此 class 的一些对象。我想让用户选择这个 class 的主要对象。看看下面的代码:
public class Program
{
public static void Main(string[] args)
{
Important imp1 = new Important("Important 1");
Important imp2 = new Important("Important 2");
Important imp3 = new Important("Important 3");
imp2.SetMostImportant();
Console.Write(Important.MostImportant.Name);
}
public class Important
{
public Important(string name)
{
Name = name;
if(MostImportant == null)
SetMostImportant();
}
public string Name { get; private set; }
public static Important MostImportant { get; private set; }
public void SetMostImportant()
{
MostImportant = this;
}
}
}
这是好的解决方案吗?如果不是,请告诉我为什么。
之前,为了实现这种事情,我只是创建了一个布尔字段,例如IsMainObject 并且,当我想更改主要对象时,我遍历了特定 class 的所有对象(或对象组)除了我想成为主要的元素,并将布尔值更改为 false,在我的新候选人中我只是将标志设置为真。示例如下:
public class Program
{
public static void Main(string[] args)
{
Important imp1 = new Important("Important 1");
Important imp2 = new Important("Important 2");
Important imp3 = new Important("Important 3");
List<Important> list = new List<Important> { imp1, imp2, imp3 };
foreach(var item in list.Where(x => x.Name != "Important 2"))
{
item.SetMostImportant(false);
}
imp2.SetMostImportant(true);
Console.Write(list.FirstOrDefault(x => x.MostImportant == true).Name);
}
public class Important
{
public Important(string name)
{
Name = name;
}
public string Name { get; private set; }
public bool MostImportant { get; private set; }
public void SetMostImportant(bool val)
{
MostImportant = val;
}
}
}
我不喜欢这个解决方案,因为:
- 我不知道
MostImportant
是否适用于多个对象而不进行迭代。 - 我需要编写额外的代码来处理更多的案例。
- 我不可能总是遍历特定 class 的所有实例(组并不总是足够)。
...还有更多,但您明白了。
public static Important MostImportant { get; private set; }
是一个很好的解决方案,比
好得多public bool MostImportant { get; private set; }
在实现 "singleton" 类 时,拥有静态类型 属性 的情况并不少见。我写了类似这样的代码:
class MyClass
{
public static MyClass Instance { get; private set; }
public MyClass()
{
if (Instance == null)
{
Instance = this;
}
else
{
throw new Exception("MyClass already instantiated.");
}
}
}