在多个类别之间共享变量 [英] Share variable between multiple classes

查看:164
本文介绍了在多个类别之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有3个班级,可以说:Mainclass,ChildClass,OtherChild。

  MainClass()
{
ChildClass cc = new ChildClass();
OtherChild oc = new OtherChild();

//设置子类的名称属性
string childName = some name;
}

ChildClass()
{
公共字符串名称{get; set;}
}

OtherChild()
{
//这里我想从ChildClass()获取name属性
//这样做将创建一个新的ChildClass实例,该实例未设置name属性。
ChildClass cc = new ChildClass();

}

此解决方案是什么?

解决方案

基本上,为了在类之间访问信息,必须在实例之间以某种方式传递该信息。



以下是使用基本设置的带注释的快速示例。我列举了一些不同的方法来在对象之间发送信息:

  public MainClass()
{
// //仅在此处使用自动属性。使用前将需要初始化。
public ChildClass cc {组; }
public OtherChild oc {get;组; }

//构造函数。初始化为新MainClass()时被调用
public MainClass()
{
//初始化属性

//选项1-初始化,然后设置
cc = new ChildClass();
cc.childName =某些名称; //设置子类

的name属性//选项2-通过构造函数
进行初始化和设置cc = new ChildClass( some name);

//选项3-使用初始化程序进行初始化和设置(更多信息:http://msdn.microsoft.com/zh-cn/library/vstudio/bb397680.aspx)
cc = new ChildClass(){name = some name};

oc =新的OtherChild(cc);
}
}

public ChildClass()
{
公共字符串名称{get;组; }

//默认构造函数。
public ChildClass()
{
}

//其他构造函数。 this.name =运行后传入 name
public ChildClass(s​​tring name)
{
//this.name指定您所引用的名称属于此类
this.name = name;
}

}

public OtherChild()
{
public ChildClass cc {get;组; }

public OtherChild()
{
cc = new ChildClass(); //在默认构造函数
中初始化对象}

public OtherChild(ChildClass childClass)
{
cc = childClass; //设置为传入的childClass的引用
}
}

当然,这些都使用.NET的自动属性。对于简单的实现,它们可以正常工作。但是,如果您需要(或只是想)拆分一个成员,下面是使用完整属性语法的示例。

  public MainClass()
{
//私有后备字段只能在此类中访问
private ChildClass _cc = new ChildClass();

//可从其他类别访问公共财产
public ChildClass cc
{
get
{
return _cc;
}
set
{
_cc =值;
}
}
}

如果您注意到,这将初始化成员声明中开头的私有 _cc 。这样可以确保在使用之前不需要显式初始化 cc 属性。同样,这比刚性标准更多的是一个例子。了解.NET使用属性和私有成员的所有方式很重要,因此您可以针对特定情况选择和使用最佳方式。






此外,作为旁注,您会注意到我包括 private public 在每个私有成员,属性和构造函数的前面。尽管在技术上不是必需的,但通常最好的做法是为每个类成员明确指定您的可访问性级别(这有助于封装)。 有关封装的Wikipedia文章有相当不错的介绍性解释和示例。 / p>

为了将来,我还建议考虑一组.NET命名约定,以了解诸如属性名称,后备字段,方法名称等内容: / p>



虽然您可以很好地阅读自己的代码,但遵循这些不同的命名约定可以确保其他人也能够阅读和理解它。


If i have 3 classes, lets say: Mainclass, ChildClass, OtherChild.

MainClass()
{
     ChildClass cc = new ChildClass();
     OtherChild oc = new OtherChild();

     //Set the name property of childclass
     string childName = "some name";
}

ChildClass()
{
    public string name {get; set;}
}

OtherChild()
{
     //Here i want to get the name property from ChildClass()
     //Doing this will make a new instance of ChildClass,  which will not have the name property set.
     ChildClass cc = new ChildClass(); 

}

What is the solution for this ?

解决方案

Basically, in order to access information from class to class, you must "pass" that information in some way between instances.

Here is a quick annotated example using your basic setup. I have included a few examples of different ways you could go about sending information between objects:

public MainClass()
{
    // just using auto-properties here. Will need initialized before use.
    public ChildClass cc { get; set; }
    public OtherChild oc { get; set; }

     // Constructor. Gets called when initializing as "new MainClass()"
     public MainClass() 
     {                
        // initialize our properties

        // option 1 - initialize, then set
        cc = new ChildClass();
        cc.childName = "some name"; //Set the name property of childclass

        //option 2 - initialize and set via constructor
        cc = new ChildClass("some name");

        // option 3 - initialize and set with initializer (more here: http://msdn.microsoft.com/en-us/library/vstudio/bb397680.aspx)
        cc = new ChildClass() { name = "some name" };

        oc = new OtherChild(cc);
     }
}

public ChildClass()
{
    public string name { get; set; }

    // Default constructor. this.name will = null after this is run
    public ChildClass() 
    {                
    }

    // Other constructor. this.name = passed in "name" after this is run
    public ChildClass(string name) 
    {
        //"this.name" specifies that you are referring to the name that belongs to this class
        this.name = name;
    }

}

public OtherChild()
{
    public ChildClass cc { get; set; } 

    public OtherChild() 
    {        
       cc = new ChildClass(); // initialize object in the default constructor
    }

    public OtherChild(ChildClass childClass) 
    {        
       cc = childClass; // set to the reference of the passed in childClass
    }
}

Of course, those all use .NET's auto-properties. For simple implementations, they work fine. If, however, you needed to (or just wanted to) split a member out, here is an example using the full property syntax.

public MainClass()
{
    // private backing field is only accessible within this class
    private ChildClass _cc = new ChildClass();

    // public property is accessible from other classes
    public ChildClass cc 
    { 
        get 
        {
            return _cc;
        }
        set
        {
            _cc = value;
        }
    }
}

If you notice, this initializes the private _cc at the beginning, in the member declaration. This ensures that the cc property does not need to be explicitly initialized before use. Again, this is more an example than a rigid standard. It's important to know all of the ways .NET uses properties and private members, so you may choose and use the best one for your particular situation.


Also, as a side note, you'll notice that I included either private or public in front of each private member, property, and constructor. While not technically necessary, it is generally good practice to explicitly specify your level of accessibility for each class member (this promotes encapsulation). The Wikipedia article on encapsulation has a pretty decent introductory explanation and examples.

For the future, I'd also suggest taking a look at a set of .NET naming conventions for things such as property names, backing fields, method names, etc:

While you may be fine reading your own code, following these different naming conventions ensures that others will be more able to read and understand it as well.

这篇关于在多个类别之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆