代码如下:
- public class ThreadDemo {
- public static void main(String[] args) {
- Demo d1 = new Demo("小强",23);
- Demo d2 = new Demo("旺财",12);
- d1.start();
- d2.start();
-
- }
- }
- class Demo extends Thread {
- private String name ;
- private int age;
-
- public Demo(String name,int a){
- this.name =name;
- int age = a;
-
- }
-
- public void run(){
- // int [] arr = new int [4];
- // System.out.println(arr[3]);
- for(int x=0;x <= 3;x++){
- System.out.println("name="+name+"&&"+"age="+age+"..."+Thread.currentThread().getName()+"***"+x);
- }
- }
-
- }
复制代码
打印结果如下:
- name=旺财&&age=0...Thread-1***0
- name=旺财&&age=0...Thread-1***1
- name=小强&&age=0...Thread-0***0
- name=旺财&&age=0...Thread-1***2
- name=旺财&&age=0...Thread-1***3
- name=小强&&age=0...Thread-0***1
- name=小强&&age=0...Thread-0***2
- name=小强&&age=0...Thread-0***3
复制代码
请问为何打印结果中的年龄都为0? |