import java.util.*;
public class Test
{
public static void main(String[] args)
{
A[] employee = new A[3];
employee[0] = new A();
employee[1] = new A();
employee[2] = new A();
for(A e: employee)
{
System.out.println(e.a);
}
}
}
class A
{
public static int a = 0;
public A()
{
a++;
}
}
The result is
but the following wording
import java.util.*;
public class Test
{
public static void main(String[] args)
{
A m = new A();
System.out.println(m.a);
A n = new A();
System.out.println(n.a);
A x = new A();
System.out.println(x.a);
}
}
class A
{
public static int a = 0;
public A()
{
a++;
}
}
get this result
I hope that the first program would be able to get the results of a second , but it is not , and why ?
------ Solution ---------------------------------------- ----
1 is a new program of three objects , and then outputs a value before the output is a + +; has been called three times.
Program 2 is a new object , the output time , which is called once for each a + +; and then outputs a value of a.
This well understood ah.
------ Solution ---------------------------------------- ----
one is immediately after printing a print is completed .
------ Solution ------------------------------------ --------
import java.util.*;
public class Test
{
public static void main(String[] args)
{
A[] employee = new A[3];
employee[0] = new A();//这里 a++ 的数值是 1
employee[1] = new A();//这里 a++ 的数值是 2
employee[2] = new A();//这里 a++ 的数值是 3
//最终你的 a 的数值是 3,所以下面你会打印3遍 3
for(A e: employee)
{
System.out.println(e.a);
}
}
}
class A
{
public static int a = 0;//注意:你这里的变量是 static,也就是说这个变量是"类"变量。
public A()
{
a++;
}
}
import java.util.*;
public class Test
{
public static void main(String[] args)
{
A m = new A();//这里 a++ 的数值是 1
System.out.println(m.a); //打印 1
A n = new A();//这里 a++ 的数值是 2
System.out.println(n.a);//打印 2
A x = new A();//这里 a++ 的数值是 3
System.out.println(x.a);//打印 3
}
}
class A
{
public static int a = 0;
public A()
{
a++;
}
}
------ Solution ------------------------------------- -------
public class TeatAll {
/**
* 这里的staticNum是static修饰的,是"类"变量。 类变量,也就是被所有的类的实例共享的。
* 如果以实例改变了这个"类变量",其他的所有实例的该数值都会变化。
*/
private static int staticNum;
/**
* 这里的 num 是没有 static 修饰的,它是类的成员变量。 成员变量,是每个类的实例所独享的。 这个是独享的,不会受到其他实例的影响。
*/
private int num;
public void showStaticNum() {
staticNum++;
System.out.println("static num : " + staticNum
+ " , 哈哈,我是"类变量",我是所有类的实例所共享的。你们其中一个类实例改变了我,所有的实例都可以看见我的变化。");
}
public void showNum() {
num++;
System.out.println("num : " + num + " , 因为我是"成员变量",所有我不会被其他的实例修改。");
}
/**
* @param args
*/
public static void main(String[] args) {
TeatAll a = new TeatAll();
a.showStaticNum();
a.showNum();
TeatAll b = new TeatAll();
b.showStaticNum();
b.showNum();
TeatAll c = new TeatAll();
c.showStaticNum();
c.showNum();
}
}
------ Solution ------------------------------------- -------
static attention to the word Mody
this no matter how many objects created object references are the same place
first example creates three objects are referenced for the last time after the assignment variable
------ For reference only ---------- -----------------------------
but similar package to the program I used to and a little puzzled.
package com.corejava;
import java.util.*;
public class Test2
{
public int id;
public static int count = 0;
public Test2()
{
count++;
id = count;
}
}
import com.corejava. *;
import java.util. *;
public class Test
{
public static void main (String [] args)
{
Test2 [] student = new Test2 [3];
student [0] = new Test2 ();
student [1] = new Test2 ();
student [2] = new Test2 ();
for (Test2 e: student)
{
System.out.println ("id =" + e.id);
}
}
}
same package is to call three times , but the result id is 1, 2, 3
------ For reference only ----- ----------------------------------
of course, if the package is not only count the words id
package com.corejava;
import java.util.*;
public class Test2
{
public static int count = 0;
public Test2()
{
count++;
}
}
output count it into the main program will really be 3 3 .
import com.corejava.*;
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Test2[] student = new Test2[3];
student[0] = new Test2();
student[1] = new Test2();
student[2] = new Test2();
for(Test2 e: student)
{
System.out.println("count = " + e.count);
}
}
}
This results
------ For reference only ----- ----------------------------------
If I understand you are looking at above . It only shows my poor language skills .
you still see "Java programming ideas ( fourth edition ) " it. Inside on the modified static static variables described in great detail . I can not compare .
------ For reference only ---------------------------------------
Thank you, I appreciate a good under . Thanks "develop_design_level" for I am anxious , I really progress very slowly .
没有评论:
发表评论