关于 this 关键字的使用

 

package com.jsti.guiyang_01;

/*
自定义Phone类
this关键字
代表当前正在调用这个方法(访问成员变量)的对象(实例)
1.在setxxx方法中用来区分成员变量和局部变量
2.在方法中指定调动本类的其它方法


标准javaBean
私有的成员变量,公有的getxxx,setxxx方法
手动定义空参的构造方法

*/
 class Phone{
    //私有成员变量
    private String brand;
    private int price;
    private String color;
    
    //公有getxxx setxxx方法
    public void setBrand(String brand){
        this.brand = brand;
    }
    public String getBrand(){
        return brand;
    }
    
    public void setPrice(int price){
        this.price = price;
    }
    public int getPrice(){
        return price;
    }
    
    public void setColor(String color){
        this.color = color;
    }
    public String getColor(){
        return color;
    }
    //构造方法:
    //与类同名,没有返回值
    public Phone(String brand,int price,String color){
        this(brand,price);//在构造方法中调用其它的构造方法,必须是构造方法的第一条语句
        System.out.println("三个参数的构造方法被调用");
        
        // this.brand = brand;
        // this.price = price;
        
        this.color = color;
        
        //return xxxx;
    }
    //构造方法:
    //与类同名,没有返回值
    public Phone(String brand,int price){
        this(brand);
        System.out.println("两个参数的构造方法被调用");
        // this.brand = brand;
        this.price = price;
    }
    //构造方法:
    //与类同名,没有返回值
    public Phone(String brand){
        System.out.println("一个参数的构造方法被调用");
        this.brand = brand;
    }
    //默认的构造方法
    public Phone(){}
    
    
    //定义show方法,用于显示所有的成员变量
    public void show(){
        System.out.println(brand +" <> "+ price +" <> "+ color);
    }
    public void test(){
        show();//this可以省略
        call("tom");
    }
    
    public void call(String name){
        System.out.println("给 " + name + " 打电话");
    }
}

public class PhoneDemo {
    public static void main(String[] args){
        /*
        Phone p = new Phone();
        p.setBrand("iPhone");
        p.setPrice(7000);
        p.setColor("red");
        System.out.println();
        // p.show();
        // p.call("Tyson");
        p.test();
        */
        
        //构造方法的调用new关键字后边实际上就是在调用构造方法
        Phone p = new Phone("iPhone",7000,"red");
        p.test();
    }
}

下段代码源自https://blog.csdn.net/fzfengzhi/article/details/2174406

* 本示例为了说明this的三种用法!

 */

package test;

public class ThisTest {

    private int i=0;

    //第一个构造器:有一个int型形参

    ThisTest(int i){

       this.i=i+1;//此时this表示引用成员变量i,而非函数参数i

       System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);

       System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));

       //从两个输出结果充分证明了i和this.i是不一样的!

    }

    //  第二个构造器:有一个String型形参

    ThisTest(String s){

       System.out.println("String constructor:  "+s);

    }

    //  第三个构造器:有一个int型形参和一个String型形参

    ThisTest(int i,String s){

       this(s);//this调用第二个构造器

       //this(i);

       /*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。

       但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调

       用一个且仅一次构造器!*/

       this.i=i++;//this以引用该类的成员变量

       System.out.println("Int constructor:  "+i+"/n"+"String constructor:  "+s);

    }

    public ThisTest increment(){

       this.i++;

       return this;//返回的是当前的对象,该对象属于(ThisTest)

    }

    public static void main(String[] args){

       ThisTest tt0=new ThisTest(10);

       ThisTest tt1=new ThisTest("ok");

       ThisTest tt2=new ThisTest(20,"ok again!");

      

       System.out.println(tt0.increment().increment().increment().i);

       //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,

       //接着又返回在上面返回的对象基础上i++的ThisTest对象!

    }

}


 

运行结果:


 

Int constructor i——this.i:  10——11

String constructor:  ok

String constructor:  ok again!

Int constructor:  21

String constructor:  ok again!

 

posted on 2018-04-15 23:49  长空剑使  阅读(220)  评论(0编辑  收藏  举报