java基础面向对象之this关键字

java基础面向对象之this关键字

2017-02-14

this的核心概念:

    this关键字在java中表示当前对象

this关键字的主要用法:

    在java中利用this关键字可以实现类属性的调用,类方法的调用,当前对象。

 

一、调用属性

 1 class Book{    //Book类
 2     private String title ;    //title属性
 3     private int price ;        //price属性
 4     
 5     public Book(String t , int p){
 6         title = t ;
 7         price = p ;
 8     }
 9     
10     public void setTitle(String title) {
11         this.title = title;
12     }
13     public String getTitle() {
14         return title;
15     }
16     public void setPrice(int price) {
17         this.price = price;
18     }
19     public int getPrice() {
20         return price;
21     }
22     public String getInfo(){
23         return "书名:"+title+",价格:"+price  ;
24     }
25 }
26 public class ThisDemo1 {
27     public static void main(String[] args) {
28         Book b = new Book("java开发基础", 22) ;
29         System.out.println(b.getInfo());
30     }
31 }

上面代码的构造方法,主要的功能就是为了给Book类的属性初始化,现在构造方法中的参数的目的是为了类中的属性初始化,那么最好的做法就是构造方法中的参数设置为与类中的属性名称保持一致。

1     public Book(String title , int price){
2         title = title ;
3         price = price ;
4     }

修改以上的参数形式就算是合理了,但是最终在构造方法传入的内容并没有传递到属性之中,

 

如果参数的属性名称与参数名称出现了重名,那么默认情况下如果没有加入任何限制,指的都是最近的“{}”内的变量名称。为了可以明确的找到要访问的变量属于类中的属性的时候,需要在变量前加上 this 关键字。

1     public Book(String title , int price){
2         this.title = title ;
3         this.price = price ;
4     }

 

二、调用方法

    从以上可以看出,所谓的this实际上指的就是本类中的属性或方法。

 1 class Book2{
 2     private String name ;
 3     private int age ;
 4     public Book2(String name  , int age ){
 5         //构造方法
 6         this.setName(name);
 7         this.setAge(age); 
 8     }
 9     public void setName(String name) {
10         this.name = name;
11     }
12     public String getName() {
13         return name;
14     }
15     public void setAge(int age) {
16         this.age = age;
17     }
18     public int getAge() {
19         return age;
20     }
21     public void print(){
22         //本类中的print方法
23         System.out.println("************");
24     }
25     public String getInfo(){//本类中的普通方法
26         this.print() ; 
27         /**
28          * 使用this关键字调用本类中的方法,也可以直接调用
29          */
30         return "个人信息" ;
31     }
32     
33 }

为了程序的严谨性,调用普通方法时一定要加上this关键字。

 

一个类中除了有普通方法还包括构造方法。这个时候要表示多个构造方法之间要进行互相调用,使用的形式就是“this(参数,参数)”

 1 class Book3{
 2     private String name ;
 3     private int age ; 
 4     public Book3(){//无参构造
 5         System.out.println("一个新的类对象产生了");
 6     }
 7     public Book3(String name){//有一个参数的构造
 8         System.out.println("一个新的类对象产生了 ");
 9         this.name = name ;
10     }
11     public Book3(String name ,int age ){//有两个参数的构造
12         System.out.println("一个新的类对象产生了");
13         this.name = name ;
14         this.age = age ; 
15     }
16 }

以上的代码实现了不管传入几个参数都会打印一句 “一个新的类对象产生了” ,但是代码出现了很多重复。那么就要解决这些重复的代码。

 1 class Book3{
 2     private String name ;
 3     private int age ; 
 4     public Book3(){//无参构造
 5         System.out.println("一个新的类对象产生了");
 6     }
 7     public Book3(String name){//有一个参数的构造
 8         this() ;
 9         this.name = name ;
10     }
11     public Book3(String name ,int age ){//有两个参数的构造
12         this(name) ;
13         this.age = age ; 
14     }
15 }

  虽然以上实现了构造方法的互相调用,但是依然存在一些限制。

    1、使用this()的形式 ,this()只可以放在方法的首行。

    2、构造方法可以调用普通方法,但是普通方法不可以调用构造方法。

    3、进行构造方法互相调用的时候,必须保留调用的出口。也就是说在构造方法互相调用的时候至少保留一个构造没有使用this()调用其他构造的情况。

 

三、表示当前对象

 1 class Book4{
 2     public void print(){
 3         /**
 4          * 那个对象调用了print()方法,this关键字指的就是那个对象
        this就是当前调用方法的对象
5 */ 6 System.out.println("this = "+ this ); 7 } 8 } 9 public class ThisDemo4 { 10 public static void main(String[] args) { 11 Book4 b1 = new Book4() ; 12 System.out.println("b1 = "+ b1 ); 13 b1.print() ; 14 System.out.println("--------------"); 15 Book4 b2 = new Book4() ; 16 System.out.println("b2 = "+ b2 ); 17 b2.print() ; 18 } 19 }

四、总结

  1、类中的属性调用以后都要加上this

  2、类中的构造方法间的互相调用,一定要保留出口。

  3、this表示当前对象,指的是当前正在调用类中方法的对象。this不是一个固定的。

posted @ 2017-02-14 14:31  西巴  阅读(400)  评论(0编辑  收藏  举报