Java This關(guān)鍵字

1.當(dāng)成員變量和局部變量重名時(shí),在方法中使用this時(shí),表示的是該方法所在類中的成員變量。(this是當(dāng)前對(duì)象自己)

/**

* Created by lenovo on 2016/3/11.

* 當(dāng)成員變量和局部變量同名時(shí),可以用關(guān)鍵字this進(jìn)行區(qū)分

* this:代表對(duì)象 (當(dāng)前對(duì)象)

*? ? ? this就是所在函數(shù)所屬對(duì)象的引用

*/

class Person

{

private String name;//為成員變量 在堆中存在

private int age;

Person(String name)//name為局部變量 在棧里存在

{

this.name=name;//沒(méi)有this的話 進(jìn)棧后有個(gè)局部變量name 此時(shí)的name和堆中的沒(méi)有關(guān)系 只不過(guò)將棧里的name賦給了自己

//this.name為成員變量 在堆中存在

}

//構(gòu)造函數(shù)用于對(duì)象初始化

public void speak(){

System.out.println(name + ":" + age);

}

}

public class Demo1 {

public static void main(String[] args)

{

Person p=new Person("Lily");

p.speak();

}

}


2.this也可以用于在構(gòu)造函數(shù)中調(diào)用其他構(gòu)造函數(shù)

/*只能定義在構(gòu)造函數(shù)的第一行 初始化動(dòng)作必須首先執(zhí)行

值得注意的是:

1:在構(gòu)造調(diào)用另一個(gè)構(gòu)造函數(shù),調(diào)用動(dòng)作必須置于最起始的位置。

2:不能在構(gòu)造函數(shù)以外的任何函數(shù)內(nèi)調(diào)用構(gòu)造函數(shù)。

3:在一個(gè)構(gòu)造函數(shù)內(nèi)只能調(diào)用一個(gè)構(gòu)造函數(shù)。

*/

class Person

{

private String name;//為成員變量 在堆中存在

private int age;

Person()

{

//this("haha");//調(diào)用Person(String name)方法

name="baby";

age=3;

System.out.println("Person run");

}

Person(String name)//name為局部變量 在棧里存在

{

this();//直接調(diào)用Person()方法

this.name=name;

}

//構(gòu)造函數(shù)用于對(duì)象初始化

Person(String name,int age)

{

//this.name=name;

this(name);//在這里找到參數(shù)值為String的構(gòu)造函數(shù)

this.age=age;

}

public void speak(){

System.out.println(name + ":" + age);

}

}


3把自己當(dāng)作參數(shù)傳遞時(shí),也可以用this.(this作當(dāng)前參數(shù)進(jìn)行傳遞)

classA {

publicA() {

newB(this).print();//調(diào)用B的方法

}

publicvoidprint() {

System.out.println("HelloAA from A!"); ? ? ? ? ? ? ? ? ? ?3 HelloAA from A!

}

}

classB {

Aa;

publicB(A a) {

this.a= a;

}

publicvoidprint() {

a.print();//調(diào)用A的方法? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1 HelloAA from A!? ? 4 HelloAA from A!

System.out.println("HelloAB from B!");? ? ? ? ? ? ? ? ? 2 HelloAB from B!? ? ? 5 HelloAB from B!

}

}

publicclassHelloA {

publicstaticvoidmain(String[] args) {

A aaa =new?A();

aaa.print();

B bbb =new?B(aaa);

bbb.print();

}

}

結(jié)果為:

1 HelloAA from A!

2 HelloAB from B!

3 HelloAA from A!//aaa.print();

4 HelloAA from A!

5 HelloAB from B!

在這個(gè)例子中,對(duì)象A的構(gòu)造函數(shù)中,用new B(this)把對(duì)象A自己作為參數(shù)傳遞給了對(duì)象B的構(gòu)造函數(shù)。


4 有時(shí)候,我們會(huì)用到一些內(nèi)部類和匿名類,如事件處理。當(dāng)在匿名類中用this時(shí),這個(gè)this則指的是匿名類或內(nèi)部類本身。這時(shí)如果我們要使用外部類的方法和變量的話,則應(yīng)該加上外部類的類名。如:

public class HelloB {

int i = 1;

public HelloB() {

Thread thread = new Thread() {

public void run() {

for (int j=0;j<20;j++) {

HelloB.this.run();//調(diào)用外部類的方法

try {

sleep(1000);

} catch (InterruptedException ie) {

}

}

}

}; // 注意這里有分號(hào)

thread.start();

}

public void run() {

System.out.println("i = " + i);

i++;

}

public static void main(String[] args) throws Exception {

new HelloB();

}

}

在上面這個(gè)例子中, thread是一個(gè)匿名類對(duì)象,在它的定義中,它的run函數(shù)里用到了外部類的run函數(shù)。這時(shí)由于函數(shù)同名,直接調(diào)用就不行了。這時(shí)有兩種辦法,一種就是把外部的run函數(shù)換一個(gè)名字,但這種辦法對(duì)于一個(gè)開(kāi)發(fā)到中途的應(yīng)用來(lái)說(shuō)是不可取的。那么就可以用這個(gè)例子中的辦法用外部類的類名加上this引用來(lái)說(shuō)明要調(diào)用的是外部類的方法run。


5this同時(shí)傳遞多個(gè)參數(shù)


publicclassTestClass {

intx;

inty;

staticvoidshowtest(TestClass tc) {//實(shí)例化對(duì)象

System.out.println(tc.x+" "+ tc.y);

}

voidseeit() {

showtest(this);

}

publicstaticvoidmain(String[] args) {

TestClass p =newTestClass();

p.x= 9;

p.y= 10;

p.seeit();

}

}


代碼中的showtest(this),這里的this就是把當(dāng)前實(shí)例化的p傳給了showtest()方法,從而就運(yùn)行了


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,871評(píng)論 18 399
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評(píng)論 19 139
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,038評(píng)論 0 11
  • 20- 枚舉,枚舉原始值,枚舉相關(guān)值,switch提取枚舉關(guān)聯(lián)值 Swift枚舉: Swift中的枚舉比OC中的枚...
    iOS_恒仔閱讀 2,439評(píng)論 1 6
  • y_浠雨閱讀 273評(píng)論 0 0

友情鏈接更多精彩內(nèi)容