java 中 this 類名 對象名 的作用

來源:互聯網
上載者:User

 

this 表示“當前對象”,不能用在靜態方法中。

對象象你知道的就有很多拉,變數拉,線程等等。

1.This用來表示全域變數
public class TestClass{  int x,y;  void testShow(int x,int y){  this.x=x; //this.x 就是我們上 int x,y;中的X 。x是我們傳來的x  this.y=y;  }} 

2.

public class TestClass {   int x;  int y;  static void showtest(TestClass tc){   System.out.println(tc.x+"  "+tc.y);  }  void seeit(){    showtest(this);  }  public static void main(String[] args){    TestClass p=new TestClass();    p.x=9;    p.y=10;    p.seeit();   }} 
這段代碼中showtest(this),這裡的this就是把當前執行個體化 的 p傳給了showtest方法,從而就運行了。

 

類名用來訪問靜態成員;

public class Test {

    private float f = 1.0f;

    int m = 12;

    static int n = 1;

    //public  static void main(String[] args) {

    public void main(String[] argc){

        Test t = new Test();

        Test.n = 10;

    }

}

對象名可以訪問靜態和非靜態成員

public class Test {

    private float f = 1.0f;

    int m = 12;

    static int n = 1;

    //public  static void main(String[] args) {

    public void main(String[] argc){

        Test t = new Test();

        Test.n = 10;

        t.m = 10;

        t.n = 10;

    }

}

聯繫我們

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