java中this的用法如:this.name=name


package com.chensi;

/**
 *  這個是為了搞懂那個  this.name = name的。
 * @author ZHL
 *
 */
public class ThisTestZhl {
    
    private int i = 0;
    
    private String s = "zhl";
    //第一個構造器
    ThisTestZhl(int i){
        System.out.println("第一個構造器");
        //這時候的this。i 表示的是成員變量的i 是0,然后this。i = i ;這時候成員變類被局部變量賦值了
        System.out.println("這個是this。i--"+(this.i));
        System.out.println("這個是i--"+i);
        System.out.println("----------");
        this.i = i;
        System.out.println("這個是i--"+i+"\n"+"這個是this。i--"+(this.i));
        System.out.println("*********");
        System.out.println("this.i----"+(this.i));
    }
    //第二個構造器
    ThisTestZhl(String s){
        System.out.println("第二個構造器");
        System.out.println("s--"+s);
        System.out.println("this.s---"+(this.s));
        this.s = s;
        System.out.println("---------");
        System.out.println("字符串構造器"+s);
        System.out.println("字符串構造器this.s--"+(this.s));
    }
    //第三個構造器 ,帶有兩個參數的構造器
    ThisTestZhl(int i ,String s){
        //this(i);
        this(s);
        System.out.println("第三個構造器");
    }
    
    public ThisTestZhl increment(){
        System.out.println("入口");
        System.out.println("入口 i---"+i);
        System.out.println("入口 this.i ---"+(this.i));
        this.i++;
        System.out.println("出口 i---"+i);
        System.out.println("出口 this.i ---"+(this.i));
        return this;
    }
    
    
    
    public static void main(String[] args) {
        ThisTestZhl tto = new ThisTestZhl(10);
        //ThisTestZhl tto2 = new ThisTestZhl("豆豆");
        //ThisTestZhl tto3 = new ThisTestZhl(10,"chensi");
        System.out.println(tto.increment().increment().increment().i);
        //tto.increment();
    }
    
    
    
    
    
    
    /* this: 通過this可以調用本類的構造方法,成員變量,成員方法
     * this() 調用構造方法
     * this.變量名  調用成員變量
     * this.方法名 調用成員方法
     */
    
    /*
     *  

        細節問題注釋已經寫的比較清楚了,這里不在贅述,只是總結一下,其實this主要要三種用法:
        
        1、表示對當前對象的引用!
        
        2、表示用類的成員變量,而非函數參數,注意在函數參數和成員變量同名是進行區分!其實這是第一種用法的特例,比較常用,所以那出來強調一下。
        
        3、用於在構造方法中引用滿足指定參數類型的構造器(其實也就是構造方法)。但是這里必須非常注意:只能引用一個構造方法且必須位於開始!
        
        還有就是注意:this不能用在static方法中!所以甚至有人給static方法的定義就是:沒有this的方法!雖然誇張,但是卻充分說明this不能在static方法中使用!
        
        
        說明在什么情況下需要用到this:
                第一、通過this調用另一個構造方法,用發是this(參數列表),這個僅僅在類的構造方法中,別的地方不能這么用。
                第二、函數參數或者函數中的局部變量和成員變量同名的情況下,成員變量被屏蔽,此時要訪問成員變量則需要用“this.成員變量名”的方式來引用成員變量。
                            當然,在沒有同名的情況下,可以直接用成員變量的名字,而不用this,用了也不為錯,呵呵。
                第三、在函數中,需要引用該函所屬類的當前對象時候,直接用this。
                            其實這些用法總結都是從對“this是指向對象本身的一個指針”這句話的更深入的理解而來的,死記不然容易忘記而且容易搞錯,要理解!
         */
    
}

結果:

第一個構造器
這個是this。i--0
這個是i--10
----------
這個是i--10
這個是this。i--10
*********
this.i----10
入口
入口 i---10
入口 this.i ---10
出口 i---11
出口 this.i ---11
入口
入口 i---11
入口 this.i ---11
出口 i---12
出口 this.i ---12
入口
入口 i---12
入口 this.i ---12
出口 i---13
出口 this.i ---13
13


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM