码迷,mamicode.com
首页 > 其他好文 > 详细

static经典案例

时间:2020-07-22 01:50:37      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:name   案例   demo   r++   work   i++   for   dem   static   

案例一:

public class A{

  //static int number = 0;

  int number = 0;

  public A(){

    number++;

    System.out.println("数字:"+number);

  }

}

public class Test{

  public static void main(String [] args){

    for(int i = 0 ; i < 10 ; i++){

      A a = new A();

    }

  }  

}

 

案例二:

public class Demo{
   public static void main(String[] args){
     Worker w1 = new Worker("Jack");
     w1.startWork();
     System.out.println("当前开工的总人数:"+Worker.totle);
     Worker w2 = new Worker("Rose");
     w2.startWork();
     System.out.println("当前开工的总人数:"+Worker.totle);
   }
}

public class Worker {
  String name;
  static int totle = 0;
  public Worker(String name){
    this.name = name;
  }
  public void startWork(){
    totle++;
    System.out.println("工人"+this.name+"开工了");
  }
}

static经典案例

标签:name   案例   demo   r++   work   i++   for   dem   static   

原文地址:https://www.cnblogs.com/Listener-wy/p/13358193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!