精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學習啦 > 知識大全 > 知識百科 > 百科知識 > java中this的用法

      java中this的用法

      時間: 玉鳳862 分享

      java中this的用法

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

        java中this的用法1

        1. this指當前對象。

        當在一個類中要明確指出使用對象變量或函數(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!");

        }

        }

        運行結(jié)果:

        s = HelloWorld!

        1 -> this.s = Hello

        2 -> this.s = HelloWorld!

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

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

        2. this作為參數(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!");

        }

        }

        運行結(jié)果:

        Hello from A!

        Hello from B!

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

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

        java中this的用法2

        一、指自己所在的對象。

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

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

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

        /**

        * @author fengzhi-neusoft

        *

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

        */

        package test;

        public class ThisTest {

        private int i=0;

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

        ThisTest(int i){

        this.i=i+1;//此時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));

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

        }

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

        ThisTest(String s){

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

        }

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

        ThisTest(int i,String s){

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

        //this(i);

        /*此處不能用,因為其他任何方法都不能調(diào)用構(gòu)造器,只有構(gòu)造方法能調(diào)用他。

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

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

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

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

        }

        public ThisTest increment(){

        this.i++;

        return this;//返回的是當前的對象,該對象屬于(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()返回一個在tt0基礎上i++的ThisTest對象,

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

        }

        }

        運行結(jié)果:

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

        String constructor: ok

        String constructor: ok again!

        Int constructor: 21

        String constructor: ok again!

        14

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

        1、表示對當前對象的引用!

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

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

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

        this的詞語用法

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

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

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

        當陳述部分的主語是this時,附加疑問部分的主語須用it。

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

      1.c中this的用法

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

      3.java標簽如何使用 如何使用java標簽

      4.java get set方法的使用

      2668017