自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

徹底填平Static坑——細(xì)節(jié)決定成敗

開發(fā) 前端
學(xué)習(xí)的過程就是填坑的過程,可不要偷懶想著跳過去,你現(xiàn)在跳過去,就相當(dāng)于給自己挖了一個(gè)坑,你遲早會(huì)掉進(jìn)去的,為了避免不掉坑,所以,努力填坑吧!

[[322828]]

學(xué)習(xí)的過程就是填坑的過程,可不要偷懶想著跳過去,你現(xiàn)在跳過去,就相當(dāng)于給自己挖了一個(gè)坑,你遲早會(huì)掉進(jìn)去的,為了避免不掉坑,所以,努力填坑吧!

如果沒有static會(huì)怎樣?

需求:

1、定義Student類

  1. 姓名、國籍,說話行為
  2. 多個(gè)構(gòu)造,重載形式體現(xiàn)

2、學(xué)生的國籍都是確定的

  1. 國籍可以進(jìn)行顯示初始化
  1. public class Student { 
  2.     String name;//姓名 
  3.     String country;//國籍 
  4.     public Student(String name, String country) { 
  5.         this.name = name
  6.         this.country = country; 
  7.     } 
  8.     public void speak(){ 
  9.         System.out.println("姓名:"+this.name+" "+"國籍:"+this.country); 
  10.     } 
  11. class Test{ 
  12.     public static void main(String[] args) { 
  13.         Student student = new Student("何道昌","中國"); 
  14.         Student student1 = new Student("孫雙雙","中國"); 
  15.         student.speak(); 
  16.         student1.speak(); 
  17.     } 
  18. ​ 

運(yùn)行結(jié)果:

姓名:何晶晶 國籍:中國

姓名:孫雙雙 國籍:中國

目前存在的問題:

現(xiàn)在我們已知學(xué)生都是中國人,現(xiàn)在我們每創(chuàng)建一個(gè)學(xué)生對(duì)象,就要給所有學(xué)生的國籍屬性賦相同的值,這樣造成堆內(nèi)存空間資源浪費(fèi)

[[322829]]

目前方案:

把“中國”這個(gè)數(shù)據(jù)移動(dòng)到數(shù)據(jù)共享區(qū)中,共享這個(gè)數(shù)據(jù)給所有的Student對(duì)象使用即可

疑問:如何才能把這個(gè)數(shù)據(jù)移動(dòng)到數(shù)據(jù)共享區(qū)中共享呢?

解決方案:

只需要用static修飾該數(shù)據(jù)即可

靜態(tài)的成員變量只會(huì)在數(shù)據(jù)共享區(qū)中維護(hù)一份,而非靜態(tài)成員變量的數(shù)據(jù)會(huì)在每個(gè)對(duì)象中都維護(hù)一份

  1. public class Student { 
  2.     String name;//姓名 
  3.     //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù) 
  4.     static String country = "中國";//國籍 
  5.     //構(gòu)造函數(shù) 
  6.     public Student(String name) { 
  7.         this.name = name
  8.     } 
  9.      //說話行為 
  10.     public void speak(){ 
  11.         System.out.println("姓名:"+this.name+" "+"國籍:"+country); 
  12.     } 
  13.  
  14. class Test{ 
  15.     public static void main(String[] args) { 
  16.         Student student = new Student("何道昌"); 
  17.         Student student1 = new Student("孫雙雙"); 
  18.  
  19.         student.speak(); 
  20.         student1.speak(); 
  21.     } 

運(yùn)行結(jié)果:

姓名:何晶晶 國籍:中國

姓名:孫雙雙 國籍:中國

下面我來詳細(xì)解說static

static(靜態(tài)修飾符)

1.static修飾靜態(tài)變量

如果有數(shù)據(jù)需要被共享給所有對(duì)象使用時(shí),那么就可以使用static修飾

靜態(tài)成員變量的訪問方式:

方式一:可以使用對(duì)象進(jìn)行訪問

格式:對(duì)象.變量名

方式二:可以使用類名進(jìn)行訪問

格式:類名.變量名

注意:

1). 非靜態(tài)的成員變量只能使用對(duì)象進(jìn)行訪問,不能使用類命進(jìn)行訪問

  1. public class Student { 
  2.     String name;//姓名     非靜態(tài)成員變量 
  3.     //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù) 
  4.     static String country = "中國";//國籍    靜態(tài)成員變量 
  5.     //構(gòu)造函數(shù) 
  6.     public Student(String name) { 
  7.         this.name = name
  8.     } 
  9.      //說話行為 
  10.     public void speak(){ 
  11.         System.out.println("姓名:"+this.name+" "+"國籍:"+country); 
  12.     } 
  13.  
  14. class Test{ 
  15.     public static void main(String[] args) { 
  16.         Student student = new Student("何道昌"); 
  17.  
  18.         System.out.println(student.name);//用對(duì)象訪問非靜態(tài)變量<br>        Systen.out.println(student.country);//用對(duì)象訪問靜態(tài)變量 
  19.         System.out.println(Student.country);//用類命訪問靜態(tài)變量 

運(yùn)行結(jié)果:

何道晶 中國

中國

2). 千萬不要為了方便訪問數(shù)據(jù)而使用static修飾成員變量,只有成員變量的數(shù)據(jù)真正需要被共享的時(shí)候,才使用static修飾

static修飾成員變量的應(yīng)用場景:如果一個(gè)數(shù)據(jù)需要被所有對(duì)象共享使用的時(shí)候,用static修飾

2.static修飾成員函數(shù)(靜態(tài)的成員方法)

靜態(tài)成員函數(shù)的訪問方式:

方式一:可以使用對(duì)象進(jìn)行訪問

格式:對(duì)象.靜態(tài)的函數(shù)名

方式二:可以使用類名進(jìn)行訪問

格式:類名.靜態(tài)的函數(shù)名

推薦使用類名直接訪問靜態(tài)的成員

原因:

1.方便

2.節(jié)省內(nèi)存

靜態(tài)函數(shù)要注意的事項(xiàng):

1.靜態(tài)函數(shù)是可以調(diào)用類名或者對(duì)象進(jìn)行調(diào)用的,而非靜態(tài)函數(shù)只能使用對(duì)象進(jìn)行調(diào)用

2.靜態(tài)的函數(shù)可以訪問靜態(tài)的成員,但是不能直接訪問非靜態(tài)的成員

3.非靜態(tài)的函數(shù)是可以直接訪問靜態(tài)與非靜態(tài)的成員

4.靜態(tài)函數(shù)不能出現(xiàn)this或者super關(guān)鍵字

  1. public class Student { 
  2.     String name;//姓名     非靜態(tài)成員變量 
  3.     //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù) 
  4.     static String country = "中國";//國籍    靜態(tài)成員變量 
  5.     //構(gòu)造函數(shù) 
  6.     public Student(String name) { 
  7.         this.name = name
  8.     } 
  9.      //說話行為 
  10.     //靜態(tài)成員方法 
  11.     public static void speak(){ 
  12.         System.out.println("國籍:"+country); 
  13.     } 
  14.     //學(xué)習(xí)行為 
  15.     //非靜態(tài)成員方法 
  16.     public  void study(){ 
  17.         System.out.println(name+"好好學(xué)習(xí)"); 
  18.     } 
  19.  
  20. class Test{ 
  21.     public static void main(String[] args) { 
  22.         Student student = new Student("何道昌"); 
  23.  
  24.         System.out.println(student.name);//用對(duì)象訪問非靜態(tài)變量 
  25.         System.out.println(student.country);//用對(duì)象訪問靜態(tài)變量 
  26.         System.out.println(Student.country);//用類命訪問靜態(tài)變量 
  27.  
  28.         student.study();//用對(duì)象訪問非靜態(tài)方法 
  29.         student.speak();//用對(duì)象訪問靜態(tài)方法 
  30.         Student.speak();//用類名訪問靜態(tài)方法 
  31.     } 

運(yùn)行結(jié)果:

何道晶

中國

中國

何道晶好好學(xué)習(xí)

國籍:中國

國籍:中國

靜態(tài)的成員變量與非靜態(tài)的成員變量的區(qū)別:

1)、作用上的區(qū)別:

1.靜態(tài)的成員變量的作用是共享一個(gè)數(shù)據(jù)給所有的對(duì)象使用

2.非靜態(tài)的成員變量的作用是描述一類事物的公共屬性

2)、數(shù)量和存儲(chǔ)位置上的區(qū)別:

1.靜態(tài)成員變量是在存儲(chǔ)方法區(qū)內(nèi)存中,而且只會(huì)存在一份數(shù)據(jù)

2.非靜態(tài)的成員變量是存儲(chǔ)在堆內(nèi)存中,有n個(gè)對(duì)象就有n份數(shù)據(jù)

3)、生命周期的區(qū)別:

1.靜態(tài)的成員變量數(shù)據(jù)是隨著類的加載而存在,隨著類文件的消失而消失

2.非靜態(tài)的成員變量數(shù)據(jù)是隨著對(duì)象的創(chuàng)建而存在,隨著對(duì)象被垃圾回收器回收而消失

靜態(tài)函數(shù)不能訪問非靜態(tài)的成員?

靜態(tài)函數(shù)只要存在有對(duì)象,那么也可以訪問非靜態(tài)的數(shù)據(jù),只是不能直接訪問。

最后,繼續(xù)用這個(gè)例子穿插一下靜態(tài)代碼塊的知識(shí)

靜態(tài)代碼塊是在Student.class文件加載到內(nèi)存的時(shí)候就馬上執(zhí)行的

  1. public class Student { 
  2.     String name;//姓名     非靜態(tài)成員變量 
  3.     //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù) 
  4.     static String country = "中國";//國籍    靜態(tài)成員變量 
  5.     //靜態(tài)代碼塊 
  6.     static
  7.         System.out.println("靜態(tài)代碼塊執(zhí)行了!!"); 
  8.     } 
  9.     //構(gòu)造函數(shù) 
  10.     public Student(String name) { 
  11.         this.name = name
  12.     } 
  13.      //說話行為 
  14.     //靜態(tài)成員方法 
  15.     public static void speak(){ 
  16.         System.out.println("國籍:"+country); 
  17.     } 
  18.     //學(xué)習(xí)行為 
  19.     //非靜態(tài)成員方法 
  20.     public  void study(){ 
  21.         System.out.println(name+"好好學(xué)習(xí)"); 
  22.     } 
  23.  
  24. class Test{ 
  25.     public static void main(String[] args) { 
  26.         Student.speak(); 
  27.     } 

運(yùn)行結(jié)果:

靜態(tài)代碼塊執(zhí)行了!!

國籍:中國

理解到這,你再看看下面的分析圖,應(yīng)該還會(huì)有所收獲

 

最后再跟大家分享一句話:

一個(gè)人若想改變自己的命運(yùn),最重要的是要改變自己,改變心態(tài),改變環(huán)境,這樣命運(yùn)也會(huì)隨之改變

責(zé)任編輯:武曉燕 來源: 泰斗賢若如
相關(guān)推薦

2011-06-15 16:22:38

2011-03-11 10:43:52

數(shù)據(jù)遷移

2017-05-11 22:58:59

2010-05-25 13:22:43

2016-07-05 10:27:30

云計(jì)算

2015-09-30 10:36:03

eSpace UC客戶華為

2010-09-27 14:30:05

評(píng)測SSL VPN

2011-03-02 09:09:53

MySQL分區(qū)管理細(xì)節(jié)

2014-10-13 13:40:07

程序員

2011-06-10 13:57:00

SEO

2014-10-13 09:47:22

程序員工作

2020-01-17 09:00:00

HashMapJava編程語言

2015-11-24 10:18:52

數(shù)據(jù)中心線纜

2011-02-14 09:32:16

ASP.NET

2019-07-31 08:56:07

故障JavaBlockingQue

2009-07-04 10:12:56

2015-06-03 14:28:21

GoogleAndroid M

2015-10-13 10:33:25

游戲品質(zhì)細(xì)節(jié)

2020-11-20 10:30:48

云計(jì)算SaaS技術(shù)

2015-11-02 09:00:54

創(chuàng)業(yè)取舍
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)