只是想我会添加你可能真的想在一个类的实例或不同的类类型之间共享......所以如果实际意图是......
A)..ALL.. ChildClass 的实例共享完全相同的名称,您可以使用“静态”您可以使用属性访问器隐藏成员是静态的事实
B)不同类型的类的不同实例需要共享信息。然后在实例化类时进行交叉引用(在此示例中,ChildClass 和 OtherChild 是不同的类类型)在此示例中,您实际上希望能够随时更改一个实例中的信息,并且仍然与其他实例...
C)此代码的“更好/更清洁”版本(更复杂但更标准): ....如果必须始终作为引用(对共享值),请考虑将成员变量更改为属性...考虑制作部分'构造函数'与其他示例一样,如果信息共享必须双向..如果有很多共享信息在两个方向上,请考虑必须指向另一个..考虑将单独的 SharedInfoClass 传递给两者构造函数,如果不同类的所有实例共享完全相同的信息,则引用静态类可以避免将 SharedInfoClass 传递给构造函数的需要被认为比“静态类”更干净(但更复杂)是单例设计模式
********** 解决方案 A ******************
// ... information shared across all instances of a class
// Class A a1,a2; a1,a2 share exact same values for certain members
class MainClass
{
void Method()
{
ChildClass cc = new ChildClass();
OtherChild oc = new OtherChild();
//Set the name property of childclass
ChildClass.s_name = "some name"; // obviously a shared static because using ChildClass.members not cc.member
cc.Name = "some name"; // now all instances of ChildClass will have this name
}
}
class ChildClass
{
// *** NOTE 'static' keyword ***
public static string s_name; // externally refered to by ChildClass.s_name
// this property hides that s_name is a static which is good or bad depending on your feelings about hiding the nature of data
public string Name
{
get
{
return s_name;
}
set // singleton so never set only created first use of get
{
s_name = value;
}
}
}
class OtherChild
{
public OtherChild()
{
}
void Method()
{
ChildClass cc = new ChildClass();
string str = cc.Name;
// cc will have the same name as the cc in MainClass
}
}
********* 解决方案 B ***************
class BMainClass
{
void Method()
{
BChildClass cc = new BChildClass();
BOtherChild oc = new BOtherChild( );
oc.m_SharedChildClassInfo = cc;
//Set the name property of childclass
cc.name = "some name"; // only this instance of BChildClass will have this name, but it visible in BOtherChild
}
}
class BChildClass
{
public string name {get; set;}
}
class BOtherChild
{
public BChildClass m_SharedChildClassInfo;
void Method()
{
BChildClass cc = m_SharedChildClassInfo;
// cc will have the same name as the cc in MainClass
// in fact it is the exact same instance as declared in MainClass so evetythng is the same
}
}
********** 解决方案 C ******************
// this one example shows both
// a set of data shared across all instances of two class
// and a set of data sharted between 2 specific classes
class CMainClass
{
void Method()
{
CSharedBetweenSome sharedSomeInstances = new CSharedBetweenSome();
CChildClass cc = new CChildClass(sharedSomeInstances);
COtherChild oc = new COtherChild(sharedSomeInstances);
//Set the name property of childclass
cc.sharedAllValue = "same name for everyone"; // ALL instance of ChildClass will have this name, ALL instances BOtherChild will be able to acess it
sharedSomeInstances.name = "sane name for cc and oc instances";
}
}
// Interface - sometimes to make things clean / standard between different classes defining an interface is useful
interface ICShareInfoAll
{
string sharedAllValue { get; set; }
}
class CSharedInto : ICShareInfoAll
{
// Singletone pattern - still an instance, rather than a static, but only ever one of them, only created first time needed
private static CSharedInto s_instance;
public static CSharedInto Instance
{
get
{
if( s_instance == null )
{
s_instance = new CSharedInto();
}
return s_instance;
}
private set // singleton never set only created first use of get
{
//s_instance = value;
}
}
// variables shared with every instance of every type of child class
public string sharedAllValue { get; set; }
}
// One child class using jointly shared and shared with all
class CChildClass : ICShareInfoAll
{
private CSharedBetweenSome sharedSomeInstance;
public CChildClass(CSharedBetweenSome sharedSomeInstance)
{
this.sharedSomeInstance = sharedSomeInstance;
}
// Shared with all
public string sharedAllValue {
get { return CSharedInto.Instance.sharedAllValue; }
set { CSharedInto.Instance.sharedAllValue = value; }
}
// Shared with one other
public string sharedAnotherInstanceValue {
get { return sharedSomeInstance.name; }
set { sharedSomeInstance.name = value; }
}
}
class COtherChild : ICShareInfoAll
{
private CSharedBetweenSome sharedSomeInstance;
public COtherChild(CSharedBetweenSome sharedSomeInstance)
{
this.sharedSomeInstance = sharedSomeInstance;
}
public string sharedAllValue {
get { return CSharedInto.Instance.sharedAllValue; }
set { CSharedInto.Instance.sharedAllValue = value; }
}
void Method()
{
string val = sharedAllValue; // shared across instances of 2 different types of class
string val2 = sharedSomeInstance.name; // currenlty shared across spefic instances
}
}