1.使用static關鍵字聲明的屬性為全局屬性
未使用static關鍵字指定city之前,如果需要將Tom,Jack,Mary三人的城市均改成Beijing,需要再次聲明三次對象的city為Beijing
1 package packageone; 2 class People{ 3 String name; 4 String city = "Shanghai"; 5 public People(String name) { 6 this.name = name; 7 } 8 public void showInfo() { 9 System.out.println("姓名:"+name+","+"城市:"+city); 10 } 11 } 12 public class StaticDemo { 13 14 public static void main(String[] args) { 15 People p1 = new People("Tom"); 16 p1.city = "Beijing"; 17 p1.showInfo(); 18 People p2 = new People("Jack"); 19 p2.city = "Beijing"; 20 p2.showInfo(); 21 People p3 = new People("Mary"); 22 //未聲明p3對象的city屬性為Beijing,則其city仍為Shanghai。 23 p3.showInfo(); 24 } 25 }
使用static關鍵字指定city後,只需設置city = “Beijing”一次,即可實現三個人城市的更改
1 package packageone; 2 class People{ 3 String name; 4 static String city = "Shanghai"; 5 public People(String name) { 6 this.name = name; 7 } 8 public void showInfo() { 9 System.out.println("姓名:"+name+","+"城市:"+city); 10 } 11 } 12 public class StaticDemo { 13 14 public static void main(String[] args) { 15 People p1 = new People("Tom"); 16 p1.city = "Beijing"; 17 p1.showInfo(); 18 People p2 = new People("Jack"); 19 p2.showInfo(); 20 People p3 = new People("Mary"); 21 //未聲明p3對象的city屬性為Beijing,但其city變為Beijing。 22 p3.showInfo(); 23 } 24 }
2.使用static關鍵字聲明的屬性和方法可直接通過類名來調用(代碼作為對該static應用的解釋有點復雜了,同時是接口的簡單使用)
1 package packageone; 2 3 interface USB { 4 void start(); 5 void stop(); 6 } 7 8 class C { 9 public static void work(USB u) { 10 u.start(); 11 System.out.println("工作中"); 12 u.stop(); 13 } 14 } 15 16 class USBDisk implements USB { 17 @Override 18 public void start() { 19 System.out.println("U盤開始工作"); 20 } 21 22 @Override 23 public void stop() { 24 System.out.println("U盤停止工作"); 25 } 26 } 27 28 class Printer implements USB { 29 @Override 30 public void start() { 31 System.out.println("打印機開始工作"); 32 } 33 34 @Override 35 public void stop() { 36 System.out.println("打印機停止工作"); 37 } 38 } 39 40 public class interfacetest { 41 public static void main(String[] args) { 42 //直接通過類名來調用work方法 43 C.work(new USBDisk()); 44 C.work(new Printer()); 45 } 46 47 }
3.注意:】使用static方法的時候,只能訪問static聲明的屬性和方法,而非static聲明的屬性和方法是不能訪問的。而非static聲明的方法是可以去調用static聲明的屬性或方法
1 package packageone; 2 //由於博主我水平有限參考了別人的代碼案例,但由於他的代碼有較大錯誤,經調試成功後援引,算是自己的代碼了吧~嘿嘿 3 class People { 4 private String name; 5 private int age; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public int getAge() { 16 return age; 17 } 18 19 public void setAge(int age) { 20 this.age = age; 21 } 22 23 // 使用static定義country屬性 24 private static String country = "China"; 25 26 // 定義static方法,修改static屬性 27 public static void setCountry(String c) { 28 country = c; 29 } 30 31 // 取得static屬性 32 public static String getCountry() { 33 return country; 34 } 35 36 // 通過構造方法為屬性賦值(初始化操作) 37 public People(String name, int age) { 38 this.name = name; 39 this.age = age; 40 } 41 42 public void info() { 43 System.out.println("姓名:"+ getName()+"年齡:"+getAge()+"城市:"+country); 44 } 45 } 46 47 public class StaticDemo { 48 public static void main(String args[]) { 49 People per1 = new People("張三", 20); 50 People per2 = new People("李四", 21); 51 People per3 = new People("王五", 23); 52 System.out.println("--------- 修改前-----------"); 53 per1.info(); 54 per2.info(); 55 per3.info(); 56 System.out.println("--------- 修改後-----------"); 57 // 直接使用類名稱調用方法來修改static屬性的內容,正是因為country為static全局變量,才不需要每個人都去修改國籍 58 People.setCountry("USA"); 59 per1.info(); 60 per2.info(); 61 per3.info(); 62 } 63 }