Java什么时候用到this关键字_java中this关键字什么时候使用

java中this关键字什么时候使用

d663f85b5883

1、当局部变量和成员变量重名的时候,在方法中使用this表示成员变量以示区分

实例:

classDemo{

String str = "这是成员变量";

void fun(String str){

System.out.println(str);

System.out.println(this.str);

this.str = str;

System.out.println(this.str);

}

}

publicclassThis{

publicstaticvoid main(String args[]){

Demo demo = newDemo();

demo.fun("这是局部变量");

}

}

2、this关键字把当前对象传递给其他方法

实例:

classPerson{

publicvoid eat(Apple apple){

Apple peeled = apple.getPeeled();

System.out.println("Yummy");

}

}

classPeeler{

staticApple peel(Apple apple){

//....remove peel

returnapple;

}

}

classApple{

Apple getPeeled(){

returnPeeler.peel(this);

}

}

publicclassThis{

publicstaticvoid main(String args[]){

newPerson().eat(newApple());

}

}

3、当需要返回当前对象的引用时,就常常在方法写return this

这种做法的好处是:当你使用一个对象调用该方法,该方法返回的是经过修改后的对象,且又能使用该对象做其他的操作。因此很容易对一个对象进行多次操作。

publicclassThis{

int i = 0;

This increment(){

i += 2;

returnthis;

}

void print(){

System.out.println("i = "+ i);

}

publicstaticvoid main(String args[]){

This x = newThis();

x.increment().increment().print();

}

}

结果为:4

4、在构造器中调用构造器需要使用this

一个类有许多构造函数,有时候想在一个构造函数中调用其他构造函数,以避免代码重复,可以使用this关键字。

d663f85b5883

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: this 关键字可以用于指向当前对象,例如: class Person { constructor(name, age) { this.name = name; this.age = age; } sayHi() { console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`); } } const john = new Person('John', 30); john.sayHi(); // 输出 "Hi, my name is John and I am 30 years old." 在这个例子,this 关键字指向当前的 Person 对象,可以使用它来访问对象的属性和方法。 ### 回答2: this关键字是用来引用当前对象的,在面向对象的编程语言经常使用。以下是一些this关键字使用例子。 例子1: 在构造函数使用this关键字 构造函数用于创建一个对象并初始化其属性。在构造函数,通过使用this关键字,可以引用当前对象。例如: ```java public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return this.name; } } Person person = new Person("张三"); System.out.println(person.getName()); // 输出:张三 ``` 例子2: 在实例方法使用this关键字 在实例方法,可以使用this关键字引用当前对象。例如: ```java public class Counter { private int count = 0; public void increment() { this.count++; } public int getCount() { return this.count; } } Counter counter = new Counter(); counter.increment(); System.out.println(counter.getCount()); // 输出:1 ``` 例子3: 在方法链式调用使用this关键字 有时候,我们希望可以链式地调用多个方法。通过在每个方法返回this,就可以实现链式调用。例如: ```java public class Calculator { private int result = 0; public Calculator add(int num) { this.result += num; return this; } public Calculator subtract(int num) { this.result -= num; return this; } public int getResult() { return this.result; } } Calculator calculator = new Calculator(); int result = calculator.add(5).subtract(3).getResult(); System.out.println(result); // 输出:2 ``` 以上是几个使用this关键字的例子,它们展示了this关键字在不同情境下的使用方法。 ### 回答3: this关键字在编程语言经常被用到,用于引用当前对象的实例。下面是一个使用this关键字的例子: 假设我们有一个名为Person的类,其有一个私有变量name和一个公有方法displayName(),用于打印出对象的姓名。在方法内部,我们使用this关键字来引用当前对象的实例,从而访问和操作该对象的属性。 以下是一个代码示例: ``` class Person { private String name; public void setName(String name) { this.name = name; } public void displayName() { System.out.println("My name is " + this.name); } } public class Main { public static void main(String[] args) { Person person1 = new Person(); person1.setName("Alice"); person1.displayName(); Person person2 = new Person(); person2.setName("Bob"); person2.displayName(); } } ``` 在上面的代码,我们创建了两个Person对象person1和person2,并分别设置了它们的姓名。当我们调用displayName()方法时,通过this.name可以访问到当前对象的name属性的值,并打印出对应的姓名。 运行这段代码,将会输出如下结果: ``` My name is Alice My name is Bob ``` 这个例子展示了如何使用this关键字来引用当前对象的实例,并对其属性进行访问和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值