一些我總會忘記的小知識總結(Android or Java),androidjava

來源:互聯網
上載者:User

一些我總會忘記的小知識總結(Android or Java),androidjava

在項目開發時過於追求大的知識,比如多線程下載,網路請求等,往往會忽略一些非常小的知識,有時候為了驗證想法和實際我會開闢一個工程專門用來驗證,接下來就是我驗證過的一些,看看你是否會忽略呢?

1. arrayList.add(null)是允許的嗎?2. ArrayList添加一個String對象,改變該String對象後再添加,結果是添加了兩個一樣的對象還是不一樣的?
ArrayList<String> strList = new ArrayList<String>();String str = "abc";strList.add(str);str = "123";strList.add(str);strList.add(null);System.out.println("strList = " + strList);
//輸出結果strList = [abc, 123, null]

所以
1. ArrayList是可以添加null對象的;
2. ArrayList添加一個String,改變值後再添加,得到的是兩個不同的String

如果你用ArrayList儲存物件,拿出來用之前記得有必要判空哦!~
而如果你需要重複添加String類型的話,定義一個String,不停覆蓋值就夠了!~

3.foreach的對象如果是空會怎樣?
ArrayList<String> strings = null;for (String str : strings) {}
//結果Exception in thread "main" java.lang.NullPointerException

所以
3.foreach的對象不可為空,否則會報null 指標

這裡就要注意如果以後傳資料的時候,如果要用到foreach的話,最好先判斷資料是否為空白!~

4.String如果初始化為null,列印出來是什嗎?
String str = null;System.out.println("str = " + str);
//輸出結果str = null

所以
4.String為null的話列印出來也是null。

如果你不希望得到這個結果的話,記得判空哦!~

5.Android 如何在初始化時擷取TextView的寬高?
tv.measure(0, 0);int width = tv.getMeasuredWidth();int height = tv.getMeasuredHeight();

在初始化過程中,預設調用 tv.getHeight()是不能得到真實高度的(得到的值為0),是因為View要等到 onLayout()結束後才能用getWidth()|getHeight()得到寬高。同樣,在onMeasure()結束後,才能得到getMeasuredWidth(),第一句tv.measure(0, 0);相當於強制調用了onMeasure(),所以二三句就能得到寬高了(注意dp和px的誤差)。

6.子類覆蓋父類的屬性探究

需要“煮個栗子”先:

栗子1:
//Father.javapublic class Father {    public String name = "Father";}//Son.javapublic class Son extends Father {    public String name = "Son"; //子類重寫了屬性}//Main.javapublic static void main(String[] args) {    //test 1    Father son = new Son(); //多態    System.out.println("son name is " + son.name);    //test 2    Son son1 = new Son();    System.out.println("son1 name is " + son1.name);}

輸出結果:

son name is Fatherson1 name is Son

【��栗子1說明】屬性不具備多態性

栗子2:
//Father.javapublic class Father {    public String name = "Father";    //父類加了一個方法    public void printName() {        System.out.println("Father: name is " + name);    }}//Son.javapublic class Son extends Father {    public String name = "Son"; //子類只重寫了屬性}//Main.javapublic static void main(String[] args) {    //test 1    Father son = new Son();    son.printName(); //調用方法列印    //test 2    Son son1 = new Son();    son1.printName(); //調用方法列印}

輸出結果:

Father: name is FatherFather: name is Father

結果一樣。

【��栗子2說明】子類預設繼承父類的方法,如果不重寫該方法,裡面使用的是變數是父類的變數。

我們需要煮第三個栗子進一步探究:

栗子3:
//Father.javapublic class Father {    public String name = "Father";    public void printName() {        System.out.println("Father: name is " + name);    }}//Son.javapublic class Son extends Father {    public String name = "Son";    //子類重寫了printName方法    public void printName() {        System.out.println("Son: name is " + name);    }}//Main.javapublic static void main(String[] args) {    //test 1    Father son = new Son();    System.out.println("--- son name is " + son.name);    son.printName(); //調用方法列印    //test 2    Son son1 = new Son();    System.out.println("--- son1 name is " + son1.name);    son1.printName(); //調用方法列印}

輸出結果:

--- son name is FatherSon: name is Son--- son1 name is SonSon: name is Son

【��栗子3說明】子類繼承父類的方法,如果重寫該方法,裡面使用的是變數是自己的變數,即使是用多態定義的。

待更新地區……

/** *         ┏┓   ┏┓ *        ┏┛┻━━━┛┻┓ *        ┃       ┃   *        ┃   ━   ┃ *        ┃ >   < ┃ *        ┃       ┃ *        ┃... ⌒ ... ┃ *        ┃       ┃ *        ┗━┓   ┏━┛ *          ┃   ┃           *          ┃   ┃   努力擠牙膏中~ *          ┃   ┃            *          ┃   ┃         *          ┃   ┃ *          ┃   ┃            *          ┃   ┗━━━┓ *          ┃       ┣┓ *          ┃       ┏┛ *          ┗┓┓┏━┳┓┏┛ *           ┃┫┫ ┃┫┫ *           ┗┻┛ ┗┻┛ */

如果你有任何問題或者建議以及你的容易忘記的小知識,請留言告訴我哦!~

著作權聲明:本文為博主原創文章,轉載請標明原文地址以及作者(阿曌)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.