【Java學習筆記之十四】Java中this用法小節

來源:互聯網
上載者:User

標籤:img   incr   簡單   ret   import   void   傳參   print   logs   

用類名定義一個變數的時候,定義的只是一個引用,外面可以通過這個引用來訪問這個類裡面的屬性和方法。

    那們類裡面是夠也應該有一個引用來訪問自己的屬性和方法納?

    呵呵,JAVA提供了一個很好的東西,就是 this 對象,它可以在類裡面來引用這個類的屬性和方法。先來個簡單的例子:

 

 1 public class ThisDemo {   2     String name="Mick"; 3     public void print(String name){ 4         System.out.println("類中的屬性 name="+this.name); 5         System.out.println("局部傳參的屬性="+name); 6     }    7     public static void main(String[] args) { 8         ThisDemo tt=new ThisDemo(); 9         tt.print("Orson");10     }11 }

 

關於返回類自身的引用,通過this 這個關鍵字返回自身這個對象然後在一條語句裡面實現多次的操作。

 

 1 public class ThisDemo {   2     int number; 3     ThisDemo increment(){ 4          number++; 5          return this; 6     }   7   private void print(){ 8          System.out.println("number="+number); 9     }10     public static void main(String[] args) {11         ThisDemo tt=new ThisDemo();12          tt.increment().increment().increment().print();13     }14 }

 

   一個類中定義兩個建構函式,在一個建構函式中通過 this 這個引用來調用另一個建構函式,這樣應該可以實現。

    這樣的實現機制在實際做應用開發的時候有會有什麼樣子的用處納?貼下寫的代碼:

 

 1 public class ThisDemo {   2     String name; 3     int age; 4     public ThisDemo (){  5         this.age=21; 6    }      7     public ThisDemo(String name,int age){ 8         this(); 9         this.name="Mick";10     }     11   private void print(){12          System.out.println("最終名字="+this.name);13          System.out.println("最終的年齡="+this.age);14     }15     public static void main(String[] args) {16        ThisDemo tt=new ThisDemo("",0); //隨便傳進去的參數17        tt.print();18     }19 }

 對this的調用必須是構造器中的第一個語句

下面給出執行個體:

 

 1 import java.util.Arrays; 2 class People 3 { 4     private int age; 5     private String name; 6     People(String name) 7     { 8         this.name=name; 9     }10     People(String name,int age)11     {12         //this.People(name);13         this(name);14         this.age=age;15         //this(age);16     }17     public void speak()18     {19         System.out.println(name+","+age);20     }21 }22 /*23 class Car24 {25     int num;26     String color;27     void run()28     {29         System.out.println(num+"---"+color);30     }31 }32 */33 class nzf34 {35     public static void main(String[] args)36     {37         People p=new People("Sakura",19);38         p.speak();39         /*40         int []a1={1,2,3,4};41         int []a2={1,2,3,4};42         int []a3={2,3,4,5};43         System.out.println(Arrays.equals(a1,a2));44         System.out.println(Arrays.equals(a2,a3));45         int []b=Arrays.copyOf(a1,6);46         for(int x:b)47         {48             System.out.print(x+" ");49         }50         System.out.println();51         System.out.println(Arrays.toString(b));52         Arrays.fill(b,2,4,1);53         System.out.println(Arrays.toString(b));54         Arrays.sort(b);55         System.out.println(Arrays.toString(b));56         */57         /*58         Car c=new Car();59         c.num=5;60         c.color="green";61         c.run();62         show(c);63         */64         /*65         new Car().num=5;66         new Car().color="red";67         new Car().run();68         */69 //        show(new Car());70     }71     /*72     public static void show(Car c)73     {74         c.num=6;75         c.color="black";76         System.out.println(c.num+"---"+c.color);77     }78     */79 }

會顯示下面這個結果:

如果交換行號13-14這段順序,改成這個:

 

        this.age=age;        this(name);

 

會出現以下錯誤:

總結一下:

   1) this 關鍵字是類內部當中對自己的一個引用,可以方便類中方法訪問自己的屬性;

   2)可以返回對象的自己這個類的引用,同時還可以在一個建構函式當中調用另一個建構函式。

   3)對this的調用必須是構造器中的第一個語句,否則會報錯

【Java學習筆記之十四】Java中this用法小節

聯繫我們

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