android開發教程(1)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   使用   java   ar   

在安卓開發過程中的一些點,這裡歸納總結一些

  1. This

    在java中,this指代當前對象的方法或成員。特別地,對於形參與當前對象成員具有相同名字時,this引用的是當前對象的成員,而不帶this的是形參。

public void setthis(string name, int id){  this.name = name; //this.name引用的是當前對象的方法或成員,而不帶this的name就是形參  this.id = id;}

  2.  Super  

用法 一:常用的用法,super一般指代的是父類的方法或成員。

public class DemoSuper extends Person{    public void print(){        System.out.println("DemoSuper:");             super.print();//調用父類的列印方法 }

用法二:調用父類構造器

class Person{    public static void prt(String s){        System.out.println(s);    }     //父類建構函式一    Person(){         prt("A Person.");    }      //父類建構函式二    Person(String name){        prt("A person name is:"+name);    }}public class Chinese extends Person{    Chinese(){        super();//調用父類建構函式一        prt("A chinese.");(1)    }     Chinese(String name){        super(name);//調用父類具有相同形參的建構函式二        prt("his name is:"+name);    }    Chinese(String name,int age){        this(name);//調用當前具有相同形參的建構函式        prt("his age is:"+age);    }    public static void main(String[] args){    Chinese cn=new Chinese();    cn=new Chinese("kevin");    cn=new Chinese("kevin",22);    }}

小結:“this指代當前對象,super指代父類”。當然,在 Chinese的各個重載建構函式中,this和super在一般方法中的各種用法也仍可使用,比如(1)處,你可以將它替換為“this.prt”(因為它繼承了父類中的那個方法)或者是“super.prt”(因為它是父類中的方法且可被子類訪問),它照樣可以正確運行。但這樣似乎就有點畫蛇添足的味道了。


參考文獻:

  1. http://blog.163.com/[email protected]/blog/static/1717240672012620111028892/

android開發教程(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.