首页 > 学院 > 开发设计 > 正文

java中this关键字用法

2019-11-17 04:04:29
字体:
来源:转载
供稿:网友
view plaincopy to clipboardPRint?
/**  
* this关键字用法  
*/  
public class Flower {   
       
    int petalCount = 0;   
    String s = "initial value";   
       
    Flower(int petals){   
        petalCount = petals;   
        System.out.println("Constructor with one int arg");   
    }   
       
    Flower(String ss){         
        System.out.println("Constructor with one string arg");   
        s = ss;   
    }   
       
    Flower(String ss, int petals){   
        this(petals);   
        this.s = ss;   
        System.out.println("Constructor with string and int args");   
    }   
       
    Flower(){   
        this("hi",47);   
        System.out.println("Default Constructor with no args");   
    }   
       
    void printPetalCount(){   
        System.out.println("petalCount = "+petalCount+"; s = "+s);   
    }   
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        // TODO Auto-generated method stub   
        Flower x = new Flower();   
        x.printPetalCount();   
    }   
}   
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表