c#(和java)中class中的static Method()和static member的用法
如public class Test{
int f1;
static int f2;//f1和f2用法比较
public static void Method1()
{...};
public void Method2()//method1和method2用法比较
{...};
}
然后再讲解下实际中static的用法(如什么情况下使用)
谢谢

解决方案 »

  1.   

    属性和方法都可以用
    类名.属性 
    类名.方法这样直接调用,不用实例化 
    比如 Test.Method1()这样 
    不用 Test t = new  Test(); t.Method1()
      

  2.   

    static,静态修饰符,有点C里面全局变量的意思。
    在JAVA中,可以看成是类的方法和属性,不需要实例化变量就可以用类直接调用。
    但是如果变量或者方法没有用static修饰,那么就只能先实例出类的变量,然后再通过该变量来调用。
    public static void main(Stringp[] args){
    /**
    *静态方法/变量
    */
       Test.f2;
       Test.Method1();
    /**
    *非静态方法/变量
    */  
       Test test = new Test();
       test.f1;
       test.Method2();
    }
      

  3.   

    "不用 Test t = new  Test(); t.Method1()"
    是"不用"还是"不能用";
    f2只能被method1()使用吗,还是method2()也能用?
      

  4.   

    问题1:可以t.Method1();但是效果与Test.Method1();是一样的。
    问题2:静态方法只能调用静态变量。但是非静态方法可以调用静态变量和非静态变量。
    所以f2可以被Method1()和Method2()调用,但是Method1()只能调用f2