java之this用法

來源:互聯網
上載者:User
必須用this關鍵字的三種情況:   1、我們想通過構造方法將外部傳入的參數賦值給類的成員變數,構造方法的形式參數名稱與類的成員變數名相同。例如:        class Person
        ...{
             String name;
             public Person(String name)
             ...{
                  this.name = name;
             } 
        }

2、假設有一個容器類和一個組件類,在容器類的某個方法中要建立組件類的執行個體對象,而組件類的構造方法要接受一個代表其所在容器的參數。例如: 

 class Container
        ...{
             Component comp;
             public void addComponent()
             ...{
                  comp = new Component(this);
             }
        }
        class Component
        ...{
             Container myContainer;
             public Component(Container c)
             ...{
                  myContainer = c;
             }
        }

3、構造方法是在產生對象時被java系統自動調用的,我們不能再程式中像調用其他方法一樣去調用構造方法。但我們可以在一個構造方法裡調用其他重載的構造方法,不是用構造方法名,而是用this(參數列表)的形式,根據其中的參數列表,選擇相應的構造方法。例如

 public class Person
        ...{
             String name;
             int age;
             public Person(String name)
             ...{
                  this.name = name;
             }
             public Person(String name,int age)
             ...{
                  this(name);
                  this.age = age;
             }
        }

 

1.this可指代對象自己本身: 當在一個類中要明確指出使用對象自己的屬性或方法時就應該加上this引用,如下例 子:

輸出的結果為:       Maybe=love you

                                   this.Maybe=love

                                   this.Maybe=love you

第一行直接對建構函式中傳遞過來的參數Maybe進行輸出,第二行是對成員變數(屬性) Maybe的輸出,第三行是先對成員變數(屬性)Maybe賦值後進行輸出。

2.在一個建構函式中調用另一個建構函式時,用this關鍵字: 

  以下例子:

   public class Flower...{  

         private int petalCount=0; 

          private String s=new String("null"); 

          public Flower(int petals)...{ 

                  petalCount=petals; 

          } 

            public Flower(String ss)...{ 

                   s=ss; 

            } 

             public Flower(String s,int petals)...{

                     this(petals); //java中在一個建構函式中可以調用一次其他的建構函式,並且這條語句 必須在這個建構函式的第一行  

                      this.s=s; 

                } 

    }

 

public class love...{

    String Maybe="love"; 

    public love(String Maybe)...{ 

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

        System.out.println("this.Maybe="+this.Maybe); 

        this.Maybe=Maybe; 

        System.out.println("this.Maybe="+this.Maybe); 

    } 

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

         love s=new love("love you"); 

    } 

 } 

 

謝:http://lw223.javaeye.com/blog/211442

俺學習

聯繫我們

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