关于 this 关键字的用法

关于 this 关键字的用法

看了thinking in Java 的第五章,才发现其中对初学者的不友好之处,在this关键字这节,其采用的代码难以理解,很复杂,却又没有好好的解释清楚,故写篇心得表达看法。

1. Peel方法的调用

//: initialization/BananaPeel.java
package initialization; /* Added by Eclipse.py */

class Banana { void peel(int i) { /* ... */ } }

public class BananaPeel {
  public static void main(String[] args) {
    Banana a = new Banana(),
           b = new Banana();
    a.peel(1);
    b.peel(2);
  }
} ///:~

首先本书就没有介绍创建一个新的对象可以中间加“逗号,”隔开来创建
压根就没有解释清楚所谓的“被a,被b调用”的问题
2. 需要返回对当前对象的引用

public class Leaf {
  int i = 0;
  Leaf increment() {
    i++;
    return this;
  }
  void print() {
    System.out.println("i = " + i);
  }
  public static void main(String[] args) {
    Leaf x = new Leaf();
    x.increment().increment().increment().print();
  }

需要明确指出对当前对象的引用,使用this关键字。
即本例:需要返回对当前对象的引用。
3. 需要将当前对象传递给其他方法(代码特别难理解

package initialization; /* Added by Eclipse.py */

class Person {
  public void eat(Apple apple) {
    Apple peeled = apple.getPeeled();
    System.out.println("Yummy");
  }
}

class Peeler {
  static Apple peel(Apple apple) {
    // ... remove peel
    return apple; // Peeled
  }
}

class Apple {
  Apple getPeeled() { return Peeler.peel(this); }
}

public class PassingThis {
  public static void main(String[] args) {
    new Person().eat(new Apple());
  }
}

讲真,这段代码真的看了好久。
不明白的地方有着几处,
①为什么需要调用Peeler.peel()方法,却没有new一个对象
②将当前对象传递给其他方法,当前对象指的是哪个?
③eat函数中(Apple apple)分别代表什么 ?
解决:Apple apple 表示传递对象的名称是apple,类型是Apple
这些都是很困扰的问题,等待以后懂了以后解决。

整段代码理解:先new Person()新建一个无对象名的对象,.eat(new Apple())表明调用eat(Apple apple)函数,其中新建一个Apple类的对象。返回类型为Apple,调用了Peel方法{还是不懂为什么没有创建Peeler对象(解决:因为peeler类是static类,可以通过类本身来调用static方法)}返回值也是apple,类型为Apple。

看懂的点:通过this在构造器中调用构造器

import static net.mindview.util.Print.*;

public class Flower {
  int petalCount = 0;
  String s = "initial value";
  Flower(int petals) {
    petalCount = petals;
    print("Constructor w/ int arg only, petalCount= "
      + petalCount);
  }
  Flower(String ss) {
    print("Constructor w/ String arg only, s = " + ss);
    s = ss;
  }
  Flower(String s, int petals) {
    this(petals);
//!    this(s); // Can't call two!
    this.s = s; // Another use of "this"
    print("String & int args");
  }
  Flower() {
    this("hi", 47);
    print("default constructor (no args)");
  }
  void printPetalCount() {
//! this(11); // Not inside non-constructor!
    print("petalCount = " + petalCount + " s = "+ s);
  }
  public static void main(String[] args) {
    Flower x = new Flower();
    x.printPetalCount();
  }
}

常见用法:将this.s代表数据成员,防止和参数发生歧义。

注意点:

  • this只能调用一个构造器
  • this调用构造器必须将调用置于最开始处
  • 除构造器之外,禁止在其他任何方法中调用构造器

static的含义:

  • static方法的内部不能调用非静态方法,例如this..
  • 可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法(这是static的主要用途)
  • static被认为不是“面向对象”的,因为不存在this,所有不是通过“向对象发送消息”的方式来完成,若代码中大量出现static方法,可以重新考虑自己的设计了。

static静态不能引用非静态方法的例子:

public class Person {
    String nameString;;

    void talk(){
//  static void talk(){   不可以使用,
//  因为不能调用nameString,nameString属于非静态的成员变量。
        System.out.println(nameString);
    }
}

为什么不可以引用呢? why
容易理解的方式:
直接调用的方法,没有创建对象。
this是指调用方法的那个对象,所以this没法解释,所以不能使用。若想引用方法只能引用静态的,所以在把name成员变量改为static类型就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值