?? ???? ? ?? ?? ??:
(?? ????: Java ?? ????)
?? ?? ?? ??
?? ?? ?? ???
?? ?? ?? ??
??:
1 . static ?? ?? ??
??: ?? ??? static ???? ???? ?? ? ??? ? ?? ?? ??? ??? ?? ?? ??? ?? ???? ????. ?? ??? ??? ???? ?????.
?? ?:
?? ???? ???? ? ?? ?? ?? ??? ?? ??? idCounter? ?????. main() ????? ? ?? ?? ?? one? two? ??????? ? ??? room?? ?? ???? two.room? ?? one.room? ?? ??? ?? ? ? ????. .
room? static ???? ???? ??? ???? ??? ? ?? ?? ??? ??? ??? ?? ??? ??? ? ??? ? ? ????. idCounter? ?? ???? ? ?? ?????? ??? ??? ??? id? 1? ?????.
public class Demo01StaticField { public static void main(String[] args) { Student one=new Student("郭靖",19); Student two=new Student("黃蓉",16); one.room="101教室"; System.out.println("姓名:"+one.getName()+",年齡:" +one.getAge()+",教室:"+one.room +",學(xué)號:"+one.getId()); //姓名:郭靖,年齡:19,教室:101教室,學(xué)號:1 System.out.println("姓名:"+two.getName() +",年齡:"+two.getAge()+",教室:"+two.room +",學(xué)號:"+two.getId()); //姓名:黃蓉,年齡:16,教室:101教室,學(xué)號:2 } } public class Student { private int id; //學(xué)號 private String name; //姓名 private int age; //年齡 static String room; //所在教室 private static int idCounter=0; //學(xué)號計數(shù)器,每當(dāng)new了一個新對象的時候,計數(shù)器++ public Student() { this.id= ++idCounter; } public Student(String name, int age) { this.name = name; this.age = age; this.id= ++idCounter; } public int getId() { return id; } public void setId(int id) { this.id = id; } //name和age的Getter,Setter方法同上id,省略 }
2. ?? ?? ?? ???
??: ?? ???? static ???? ???? ????? ?? ???? ???. ?? ???? ??? ??? ?? ???? ????.
?? ?? ?? ???? ??: ??? ??? ????? ??? ??? ??? [??? ??]? ?? ?? ??? ? ????.
?? ?:
?? ??, ?? ?? ??, ?? ??? ? ?? ?? ???? ?? ? Myclass ???? ????. ?? ???? ?? ??? ?? ?? ??? ???? ? ??? ? ? ????. ?? ???? ?? ???? ???? ? ??, ??? ???? ???? ? ???, this ???? ??? ? ????. ? ???? ???? ?? static ???? ???? ??? ??? ?? ?? ??? ??? ?? ?? ??? ? ????. ? ???? ?? ???? ?? ??? ??? ??? ? ????.
public class Demo02StaticMethod { public static void main(String[] args) { //非靜態(tài)方法使用:1.首先創(chuàng)建對象 MyClass obj=new MyClass(); //2.然后才能使用沒有static關(guān)鍵字的方法 obj.method(); //對于靜態(tài)方法來說,可以通過對象名進行調(diào)用,也可以通過類名稱來調(diào)用。 obj.methodStatic(); //正確,不推薦,這種寫法也會被javac翻譯成“類名稱.靜態(tài)方法名” MyClass.methodStatic(); //正確,推薦 //對于本類當(dāng)中的靜態(tài)方法,可以省略類名稱 myMethod(); Demo02StaticMethod.myMethod(); //完全等效 } public static void myMethod(){ System.out.println("自己的方法!"); } } public class MyClass { int num; //成員變量 static int numStatic; //靜態(tài)變量 //成員方法 public void method(){ System.out.println("這是一個普通的成員方法。"); //成員方法可以訪問成員變量 System.out.println(num); //成員方法可以訪問靜態(tài)變量 System.out.println(numStatic); } //靜態(tài)方法 public static void methodStatic(){ System.out.println("這是一個普通的靜態(tài)方法。"); //靜態(tài)方法可以訪問靜態(tài)變量 System.out.println(numStatic); //靜態(tài)不能直接訪問非靜態(tài)【重點】 //System.out.println(num); //錯誤寫法 //靜態(tài)方法中不能使用this關(guān)鍵字 //System.out.println(this); //錯誤寫法 } }
(???? ?? : java ??)
3. static static code block
?? :
public class 類名稱{ static{ //靜態(tài)代碼塊的內(nèi)容 } }
?? : ? ???? ?? ??? ? static ?? ??? ? ?? ?????. ???? ??? ????? ? ??? ? ????. ???? ?? ?? ?? ?? ?? ?? ?? ?? ???? ? ?? ???? ?? ??? ??(?? ??? ???) ??? ?????. ??: ?? ???? ?? ??? ????? ????? ?? ?? ??? ????? ?? ?????.
public class Demo04StaticCode { public static void main(String[] args) { Person one=new Person(); System.out.println("************************"); //無論創(chuàng)建幾個Person對象,靜態(tài)代碼塊只執(zhí)行一次 Person two=new Person(); } } public class Person { static{ System.out.println("靜態(tài)代碼塊執(zhí)行!"); } public Person() { System.out.println("構(gòu)造方法執(zhí)行!"); } }
?? ??:
? ??? static ???? ? ?? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

C ???? static ???? ??? ???? ?? ?? ?? C ???? static? ??, ?? ? ??? ??? ???? ? ??? ? ?? ?? ??? ??????. static ???? ???? ??? ?? ??, ??, ????? ??? ? ????. C ???? static ???? ??? ???? ??? ??? ?????. ?? ?? ? ??: ?? ??? static ???? ???? ??? ??? ?? ?? ??? ?? ?? ???? ???.

C ???? ??? ?? ? ???: 1. ?? ??, 3. ?? ??, 5. ?? ??, 1. ?? ??, ?? ?? static ???? ?? ?? ??? ??? ??? ??? ??? ?????. ?, ??? "?? ?? ??"??? " ?? ??" ?? ?? 2. ????, ?? ??? ???? ??? ??? ? ? ? ????? ????? ??? ?????.

PHP?? var ???? ??? ? PHP??? var ???? ???? ??? ?????. ?? PHP ????? var ???? ???? ?? ?? ??? ???? ???? ?????? ? ?? ??? ???? ????. ??? ?? ???? var ???? ?? ?????. var ???? ?? ?? ??? ???? ? ???? ?? ??? ???? ?? ??? ?????. ?, ??? ?? ?? ?? ???? ? ? ??? ?? ??? ?? ????? ???? ? ????. var ??

??: C????? go? ??????? ?? ?? C ???? "go"? ???? ????. C ??? ???? C ??? ?? ???? ?? ?? ??? ??? ???? ? ?????. ?? ?????? ??? ??? ??? ???? ?? ???? ??? ? ????. ?? ??, ??? "int"? ?? ??? ??? ???? "if"? ???? ???? ????. "go"? C ??? ????? ????? ??? ????? ???? ???? ? ????. ?? ??? ????: #inc

Go ???? while? ???? ????. "for {sum++ if sum>10{break}else{...}}"? ?? for ?? break? ?? ???? while ??? ??? ?? ? ????. go ???? break, default, func, select, case, defer, go, map, else, goto, for, if, var ?? ?? 25?? ???? ????.

C ?? static ???? ?? ?? ???? ? ?? ?? 1. ?? static? C ???? ??? ??? ???? ? ???? ??????. ? ??? ???? ?? ?? ?? ??? ???? ???? ??? ??? ???? ??? ????. ? ????? static ???? ?? ?? ????? ?? ??? ???? ???? ?? ??? ?? ?????. 2. ?? ??? ??? ?? ??? ?????. static ???? ???? ?? ??? ???? ?? ??? ?????.

1. static ?? ?? ????? ??????. publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} ??? ?????. ???? ????? Java? ??? ???? ????? ?????. Java? ??? ???? C ? ?? ?? ??? ????? ? ??? ??? ??? ? ??? ???. ??? "Hello, world"? ???? ?? ??? ????. ??? ?? ???? ?? ??? ?????.

C ???? 32?? ???? ????. ???? ??? ?? ??? ?? ???, ??? ???, ?? ?? ??? ? ?? ???? ? ?? ??? ?? ? ????. char, double, float, int ?? ??? 12?? ??? ?? ???? ????. for, break, if, else, do ?? ??? 12?? ??? ???? ????. auto, static, extern ? const, sizeof ?? ??? 4?? ?? ???? ????.
