当前位置: 首页 > news >正文

java中this. 和 this::的区别和用法

在Java中,this.this:: 是两个不同的概念,有着不同的用法和含义:

1. this. 的用法和含义

含义this 关键字代表当前对象的引用,this. 用于明确访问当前对象的成员变量或成员方法。在以下几种场景中,经常会用到 this.

访问成员变量
当局部变量和成员变量同名时,为了区分二者,需要使用 this. 来访问成员变量。例如:

public class Person {private String name;private int age;public Person(String name, int age) {// 这里的name和age是构造方法的参数,也是局部变量// 通过this.来明确访问成员变量this.name = name; this.age = age;}public void introduce() {System.out.println("My name is " + this.name + ", and I'm " + this.age + " years old.");}
}

在上述代码的构造方法中,使用 this.namethis.age 明确表示要操作的是类的成员变量,而不是与成员变量同名的局部变量。

调用成员方法
在一个成员方法中,调用当前对象的其他成员方法时,this. 通常可以省略,但在某些情况下,比如需要明确强调是当前对象的方法,或者在匿名内部类中访问外部类的成员方法时,会使用 this.。例如:

public class Animal {public void eat() {System.out.println("Animal is eating.");}public void drink() {// 这里可以省略this.,但加上也能明确表示是当前对象的eat方法this.eat(); System.out.println("Animal is drinking.");}
}

作为方法参数传递
可以将 this 作为参数传递给其他方法,以便在其他方法中操作当前对象。例如:

public class Car {private String brand;public Car(String brand) {this.brand = brand;}public void showBrand(Car car) {System.out.println("The brand of the car is " + car.brand);}public void introduce() {// 将当前对象this作为参数传递给showBrand方法showBrand(this); }
}

2. this:: 的用法和含义

含义this:: 是Java 8中引入的方法引用语法的一种形式,它表示对当前对象实例方法的引用, 用于将方法作为参数传递给其他方法,通常结合函数式接口一起使用。

使用场景
在使用函数式接口的地方,当你想传递一个方法而不是一个Lambda表达式时,可以使用方法引用。例如,在 java.util.stream.Stream 的操作中:

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;class Product {private String name;private double price;public Product(String name, double price) {this.name = name;this.price = price;}public void printDetails() {System.out.println("Product: " + name + ", Price: " + price);}
}public class MethodReferenceExample {public static void main(String[] args) {List<Product> productList = new ArrayList<>();productList.add(new Product("Book", 19.99));productList.add(new Product("Pen", 2.99));// 使用this::printDetails方法引用productList.forEach(this::printDetails); }
}

在上述代码中,productList.forEach(this::printDetails); 表示对 this(当前对象,这里是 MethodReferenceExample 的实例 ,虽然在 main 方法中,this 指向的是 MethodReferenceExample 的实例)的 printDetails 方法的引用,它等价于 productList.forEach(product -> product.printDetails()); 这样的Lambda表达式。

3. 两者的区别

  • 本质不同this. 主要用于访问当前对象的成员变量和成员方法,是对对象成员的直接访问操作;而 this:: 是一种方法引用语法,用于将当前对象的实例方法作为一个可传递的对象(类似函数式编程中的函数传递),传递给其他需要函数式接口作为参数的方法。
  • 语法和使用场景不同this. 广泛应用于类的各种成员方法中,用于明确访问对象成员;this:: 则主要出现在需要使用函数式接口的地方,比如在流操作、线程构造等场景中传递方法。
http://www.dtcms.com/a/266510.html

相关文章:

  • Apache RocketMQ进阶之路阅读笔记和疑问
  • RabbitMQ用法的6种核心模式全面解析
  • 论文解析:AutoMedPrompt框架的核心与实现示例
  • 【Qt】在windows环境下,配置QtCreator中的clang-format
  • P/Invoke 在默认封送(marshalling)规则下,常见托管 ⇄ 非托管类型的对应关系
  • Jenkins-Publish HTML reports插件
  • Oracle DB和PostgreSQL,OpenGauss主外键一致性的区别
  • 强化学习 (10)蒙特卡洛
  • SRE - - PV、UV、VV、IP详解及区别
  • Web基础关键_008_JavaScript 的 BOM、ES6、构造函数、原型
  • 利用 AI 打造的开发者工具集合
  • 【Unity笔记02】订阅事件-自动开门
  • 模型部署与推理--利用libtorch模型部署与推理
  • Redisearch接入SpringBoot项目使用
  • MySQL 中 -> 和 ->> 操作符的区别
  • github上部署自己的静态项目
  • 【狂飙AGI】第7课:AGI-行业大模型(系列1)
  • jsonCPP 开源库详解
  • CentOS配置网络
  • RocketMQ延迟消息是如何实现的?
  • 深度学习基础1
  • 基于Android的财务记账App
  • 【wps】 excel 删除重复项
  • AI 应用于进攻性安全
  • linux_git的使用
  • MySQL 8.0:窗口函数
  • 【Unity开发】Unity实现对模型移动、缩放、旋转操作的功能
  • 基于Docker构建OrangePi5 SDK环境
  • 408第三季part2 - 计算机网络 - 计算机网络基本概念
  • 闲庭信步使用SV搭建图像测试平台:第二十九课——绘制正弦波的图片