09.this关键字

版权声明:原创文章,转载请声明原处 https://blog.csdn.net/qq_41723615/article/details/86221394

在Java中this可以完成3件事情:

  • 调用本类属性
  • 调用本类方法
  • 表示当前对象
     

范例:调用本类属性

//this调用属性
class Book{
    private String title;
    private double price;
    public Book(String title,double price){
        title=title;
        this.price=price;
    }
    //setter,getter 略
    public String getInfo(){
             return"书名:"+this.title+",价格:"+this.price;
    }
}
public class TestThis{
    public static void main(String args[]){
        Book book=new Book("Java开发",89.2);
        System.out.println(book.getInfo());
    }
}
//只要是访问类中的属性前面必须加上"this."

为了减少不必要的麻烦,在类中访问属性时不管是否有重名的变量,一定耍加上“this” 。

本程序在Book类中直接定义了一个构造方法,其目的是希望通过构造方法为title和price两个属性进行赋值, 但最终的结果发现并没有成功赋值, 这是因为在Java中的变f,t使用具备“就近取用” 的原则.在构造方法中已经存在title或price变盐名称,所以如果直接调用title或price变量将不会使用类中的属性, 只会使用方法中的参数;

范例:调用本类的方法

//this调用方法  this(参数,参数)
class Book{
    private String title;
    private double price;
    public Book(){
        System.out.println("一个新的Book类对象产生");
    }
    public Book(String title){
        this();   //调用本类的无参构造
        this.title=title;
        
    }
    public Book(String title,double price){
        this(title);  //调用本类的单参构造
        this.price=price;
         
    }
    //setter,getter 略
    public String getInfo(){
             return"书名:"+this.title+",价格:"+this.price;
    }
}
public class TestThis{
    public static void main(String args[]){
        Book book=new Book("Java开发",89.2);
        System.out.println(book.getInfo());
    }
}

本程序在getlnfo()方法进行对象信息取得前, 调用了本类中定义的print()方法, 由于是在本类定义
的, 所以可以直接利用“ this.方法。” 的形式进行访问。

实例:定义一个雇员类(编号、姓名、工资、部门), 在这个类里提供了以下4个构造方法。
        无参构造:编号为0, 姓名为无名氏, 工资为00, 部门设置为“未定” ;
        单参构造(传递编号):姓名为“临时工” , 工资为800.0, 部门为后勤:
        双参构造(传递编号、姓名):工资为2000.0, 部门为技术部;
        四参构造。

class Emp{
    private int empno;
    private String ename;
    private double sal;
    private String dept;
    public Emp(){
         this(0,"无名氏",0.0,"未定");
    }
    public Emp(int empno){
        this(empno,"临时工",800.0,"后勤部");
    }
    public Emp(int empno,String ename){
         this(empno,ename,2000.0,"技术部");        
    }
    public Emp(int empno,String ename,double sal,String dept){
        this.empno=empno;
        this.ename=ename;
        this.sal=sal;
        this.dept=dept;
    }
    public String getInfo(){
        return"雇员编号:"+this.empno+",姓名:"+this.ename+",工资:"+this.sal;
    }
}
public class TestThis2{
    public static void main(String args[]){
         Emp ea=new Emp();
         Emp eb=new Emp(7369);
         Emp ec=new Emp(7566,""ALLEN);
         Emp ed=new Emp(7839,"KING",5000.0,"财务部");
         System.out.println(ea.getInfo());
         System.out.println(eb.getInfo());
         System.out.println(ec.getInfo());
         System.out.println(ed.getInfo());
    }
}

范例:调用当前对象

class Book{
    public void println(){
       //哪个对象调用了print方法,this就自动与此对象指向同一块内存地址
       //this就是当前调用方法的对象
        System.out.println("this="+this);
    }

    public class TestThis3{
         public static void main(String args[]){
            Book booka=new Book();
            System.out.println("booka="+booka);
            System.out.println("------------------");
            Book bookb=new Book();
            System.out.println("bookb="+bookb);
         }

    }
}

可以发现每个对象都有一个独立的对象编码, 而在使用不同的对象调用print()方法时,this 的引用也会发生改变, 即
this为当前对象。

 

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/86221394