Java this关键字用在构造方法中


this 关键字用在构造方法中。

[java]
  1. package com.han;  
  2.   
  3. /** 
  4.  * this 关键字用在构造方法中。 
  5.  * @author han 
  6.  * 
  7.  */  
  8. public class ThiSUSE {  
  9.   
  10.     public ThisUse() {  
  11.         this("this调用无参构造方法之前先调用有参构造方法");  
  12.         //it is equivalent to : new ThisUse("this调用无参构造方法之前先调用有参构造方法");   
  13.         System.out.println("this调用无参构造方法");  
  14.     }  
  15.     public ThisUse(String name) {//构造方法重载   
  16.         System.out.println(name);  
  17.     }  
  18.   
  19.     @SuppressWarnings("unused")  
  20.     public static void main(String[] args) {  
  21.         // TODO Auto-generated method stub   
  22.         ThisUse object=new ThisUse();  
  23.     }  
  24.   
  25. }  

相关内容