static的java用法

package staticer;

class person{

    int age,height;

    String name;

    person()

    {

        total++;

    }

    person(int a,int h,String n)

    {

        name=n;

        height=h;

        age=a;

        total++;

    }

    static int total=5;

    static void die()

    {

        total--;

        System.out.println(total);

        System.out.println("死人了");

    }

    static void destroy()

    {

        total=0;

        System.out.println(total);

        System.out.println("人类毁灭了");

    }

}

public class staticer {

    public static void main(String[] args) {

        System.out.println(person.total);

        person p=new person();

        p.age=15;

        p.height=170;

        p.name="fdfsds";

        System.out.println(p.total);

        person p1=new person(11,167,"liming");

        System.out.println(p1.total);

        p1.die();

        p1.destroy();

    }

}

结果如下

5

6

7

6

死人了

0

人类毁灭了

原理:

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/81148271