In another class, a class of variables from the method

In another class, a class of variables from the method

One, no need to instantiate Examples

public class A
{
   // a是静态变量(static),称为类变量。类变量无需实例化就可以用了;
    public static int a;
}
 
// 在类B中使用类A中的变量
public class B
{
    public void Test1()
    {
        //存取A.a,不用实例化
        A.a= 56;
        Console.WriteLine(A.a);
    }
}

Second, the need to instantiate Examples

public class A
{
    // b是不是静态变量,称为实例变量,只能通过实例使用。
    public int b;
}
 
// 在类B中使用类A中的变量
public class B
{
    public void Test1()
    {
        //存取A中的实例变量x,必须先实例化A,然后才能使用
        A a = new A();
        a.b = 70;
        Console.WriteLine(a.b);
    }
}
Published 13 original articles · won praise 1 · views 146

Guess you like

Origin blog.csdn.net/weixin_43363236/article/details/103601991