static经典案例
Posted listener-wy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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经典案例的主要内容,如果未能解决你的问题,请参考以下文章
0507 构造代码块和static案例,接口interface