国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 知識(shí)大全 > 知識(shí)百科 > 百科知識(shí) > java中this的用法

    java中this的用法

    時(shí)間: 玉鳳862 分享

    java中this的用法

      This,英語單詞,發(fā)音:[英][ðɪs][美][ðɪs]。常翻譯為:這,這么。java中this的用法有哪些呢?本文是學(xué)習(xí)啦小編整理java中this的用法的資料,僅供參考。

      java中this的用法1

      1. this指當(dāng)前對象。

      當(dāng)在一個(gè)類中要明確指出使用對象變量或函數(shù)時(shí)加上this引用。如下面例子中:

      public class Hello {

      String s = "Hello";

      public Hello(String s){

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

      System.out.println("1 -> this.s = " + this.s);

      this.s = s;

      System.out.println("2 -> this.s = " + this.s);

      }

      public static void main(String[] args) {

      Hello x=new Hello("HelloWorld!");

      }

      }

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

      s = HelloWorld!

      1 -> this.s = Hello

      2 -> this.s = HelloWorld!

      在這個(gè)例子中,構(gòu)造函數(shù)Hello中,參數(shù)s與類Hello的變量s同名,這時(shí)直接對s進(jìn)行操作則是對參數(shù)s進(jìn)行操作。對類Hello的成員變量s進(jìn)行操作就應(yīng)該用this進(jìn)行引用。運(yùn)行結(jié)果的第一行就是直接對構(gòu)造函數(shù)中傳遞過來的參數(shù)s進(jìn)行打印結(jié)果;

      第二行是對成員變量s的打印;第三行是先對成員變量s賦傳過來的參數(shù)s值后再打印,所以結(jié)果是HelloWorld!

      2. this作為參數(shù)傳遞

      當(dāng)你要把自己作為參數(shù)傳遞給別的對象時(shí)如:

      public class A {

      public A() {

      new B(this).print();

      }

      public void print() {

      System.out.println("Hello from A!");

      }

      }

      public class B {

      A a;

      public B(A a) {

      this.a = a;

      }

      public void print() {

      a.print();

      System.out.println("Hello from B!");

      }

      }

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

      Hello from A!

      Hello from B!

      在這個(gè)例子中,對象A的構(gòu)造函數(shù)中,new

      B(this)把對象A作為參數(shù)傳遞給了對象B的構(gòu)造函數(shù)。

      java中this的用法2

      一、指自己所在的對象。

      比如在一搜索個(gè)方法中,調(diào)用其他對象的變量或方法時(shí),可以使用那個(gè)對象的對象名,比如aa.abc();

      而調(diào)用自己所在對象的方法或變量時(shí),不知道別人給起了什么名,所以直接用this.abc()就可以了。

      二、看一個(gè)小例子中“this”的用法!

      /**

      * @author fengzhi-neusoft

      *

      * 本示例為了說明this的三種用法!

      */

      package test;

      public class ThisTest {

      private int i=0;

      //第一個(gè)構(gòu)造器:有一個(gè)int型形參

      ThisTest(int i){

      this.i=i+1;//此時(shí)this表示引用成員變量i,而非函數(shù)參數(shù)i

      System.out.println("Int constructor i——this.i: "+i+"——"+this.i);

      System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));

      //從兩個(gè)輸出結(jié)果充分證明了i和this.i是不一樣的!

      }

      // 第二個(gè)構(gòu)造器:有一個(gè)String型形參

      ThisTest(String s){

      System.out.println("String constructor: "+s);

      }

      // 第三個(gè)構(gòu)造器:有一個(gè)int型形參和一個(gè)String型形參

      ThisTest(int i,String s){

      this(s);//this調(diào)用第二個(gè)構(gòu)造器

      //this(i);

      /*此處不能用,因?yàn)槠渌魏畏椒ǘ疾荒苷{(diào)用構(gòu)造器,只有構(gòu)造方法能調(diào)用他。

      但是必須注意:就算是構(gòu)造方法調(diào)用構(gòu)造器,也必須為于其第一行,構(gòu)造方法也只能調(diào)

      用一個(gè)且僅一次構(gòu)造器!*/

      this.i=i++;//this以引用該類的成員變量

      System.out.println("Int constructor: "+i+"/n"+"String constructor: "+s);

      }

      public ThisTest increment(){

      this.i++;

      return this;//返回的是當(dāng)前的對象,該對象屬于(ThisTest)

      }

      public static void main(String[] args){

      ThisTest tt0=new ThisTest(10);

      ThisTest tt1=new ThisTest("ok");

      ThisTest tt2=new ThisTest(20,"ok again!");

      System.out.println(tt0.increment().increment().increment().i);

      //tt0.increment()返回一個(gè)在tt0基礎(chǔ)上i++的ThisTest對象,

      //接著又返回在上面返回的對象基礎(chǔ)上i++的ThisTest對象!

      }

      }

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

      Int constructor i——this.i: 10——11

      String constructor: ok

      String constructor: ok again!

      Int constructor: 21

      String constructor: ok again!

      14

      細(xì)節(jié)問題注釋已經(jīng)寫的比較清楚了,總結(jié)一下,其實(shí)this主要要三種用法:

      1、表示對當(dāng)前對象的引用!

      2、表示用類的成員變量,而非函數(shù)參數(shù),注意在函數(shù)參數(shù)和成員變量同名是進(jìn)行區(qū)分!其實(shí)這是第一種用法的特例,比較常用,所以那出來強(qiáng)調(diào)一下。

      3、用于在構(gòu)造方法中引用滿足指定參數(shù)類型的構(gòu)造器(其實(shí)也就是構(gòu)造方法)。但是這里必須非常注意:只能引用一個(gè)構(gòu)造方法且必須位于開始!

      還有就是注意:this不能用在static方法中!所以甚至有人給static方法的定義就是:沒有this的方法!雖然夸張,但是卻充分說明this不能在static方法中使用!

      this的詞語用法

      adj.(形容詞)this用作形容詞作“這”解時(shí),用于修飾表示在時(shí)間、地點(diǎn)、想法上更接近講話者的事物或人,也可與包括現(xiàn)在的日子或一段時(shí)間的詞語連用。

      “this+one's+ n. ”是一種簡潔的文體,有強(qiáng)調(diào)意味; “this+基數(shù)詞+時(shí)間名詞”表示一段時(shí)間。this可與of短語連用,后接名詞性物主代詞或名詞所有格。

      pron.(代詞)this用作代詞可用以指敘述中的人或事物,即指前面提到過的人或事物或下文提及的事物; this一般作主語時(shí)才指人; 在電話用語中, this用來指代自己。

      當(dāng)陳述部分的主語是this時(shí),附加疑問部分的主語須用it。

    看了java中this的用法的人還看了:

    1.c中this的用法

    2.this的復(fù)數(shù)和用法例句

    3.java標(biāo)簽如何使用 如何使用java標(biāo)簽

    4.java get set方法的使用

    2668017