我想将单例类成员与现有值相关联。这与单身人士的想法有冲突吗? 例如:
class Point
{
private int x;
private int y;
}
我希望单例实例与Point实例
的值相关class PointSingleton //This is the singleton class
{
private static PointSingleton item;
private Point point; //How to relate this member to an existing point values?
static PointSingleton() //Static Ctor to initialize the item instance
{
item = new p();
}
private p()//private Ctor
{
}
public static PointSingleton GetPointSingleton()// method that enable access to item
{
return item;
}
}
从另一个类访问可能看起来像:
PointSingleton instance = PointSingleton.GetPointSingleton();
但是我可以在哪里插入所需的值?
谢谢。
答案 0 :(得分:0)
如果要编辑Point.x和Point.y,只需将一个公共更改方法添加到PointSingleton
class PointSingleton
{
...
public static UpdatePointValues(int x, int y)
{
point.x = x;
point.y = y;
}
}
这将更改PointSingleTon类的所有用户的基础Point的值。您可能还需要一个公共get属性来从PointSingleton获取Point。