Home Java javaTutorial Records vs Classes in Java

Records vs Classes in Java

Oct 17, 2024 am 06:09 AM

Records vs Clases en Java

如果您已经了解 Java 中的 records,您可能会发现它的用法与类非常相似,但必须考虑到一些重要的差异。在本文中,我们将了解 Java 中记录之间的差异。如果您仍然不知道记录,我建议阅读我的文章《Java 中的记录:它们是什么以及如何使用它们》。

不变性

不可变对象是指一旦创建对象,其属性就无法修改的对象。对于 records 来说,它们是不可变的,也就是说,一旦创建 record 类型的对象,其属性就无法修改。另一方面,类可能是不变的,也可能不是不变的,具体取决于它的实现方式。这部分确保数据的完整性并防止其被意外修改。

目的

类通常只是为了存储数据而编写,例如来自数据库查询的数据或来自表单的数据。在许多情况下,该数据是不可变的,因为需要在不使用同步的情况下确保数据的有效性。为了实现这一点,使用以下元素编写一个类:

  • 每个字段的私有属性。
  • 每个字段的获取器。
  • 初始化所有字段的构造函数。
  • 比较对象是否相等的 equals 方法。
  • hashCode 方法,根据字段生成哈希码。
  • 一个 toString 方法,用于生成字段的字符串表示形式。

例如,如果你有一个 Person 类,有两个属性 name 和 lastName,你可以这样写:

public class Person {

    private final String name;
    private final String lastName;

    public Person(String name, String lastName) {
        this.name = name;
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return "Person{" + "name='" + name + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person person)) return false;
        return Objects.equals(getName(), person.getName()) && Objects.equals(getLastName(), person.getLastName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getLastName());
    }
}
Copy after login

这是该任务的解决方案,但它是实际需要的大量代码。如果类具有更多属性,即使在 IDE 或 GitHub Copilot 等插件的帮助下完成,编写的代码也会更长。更好的解决方案是将我们的类声明为数据类,即仅存储数据的类,并且不必具有特定行为,这就是 records 的用武之地。

这样,Person 类就可以重写为记录,如下所示:

public record Person(String name, String lastName) { }
Copy after login

这会自动生成 equals、hashCode 和 toString 方法,以及每个属性的 getter 方法。

记录和类之间有什么区别?

  • 不可变性记录是不可变的,也就是说,记录类型的对象一旦创建,其属性就无法修改。相反,类可能是不变的,也可能不是不变的,具体取决于它的实现方式。
  • 生成方法记录自动生成equals、hashCode和toString方法,以及每个属性的getter方法。另一方面,在类中,这些方法必须手动实现或借助 IDE 实现。
  • 在OOP中使用记录不能继承其他类,也不能被其他类扩展,但可以实现接口。另一方面,类可以从其他类继承、扩展,并且通常非常适合涵盖面向对象编程的概念。
  • 语法记录的语法比类更简单,因为它可以在一行中定义,而类需要多行代码。
  • 用途记录是一种类似于DTO(数据传输对象)的结构,即帮助建模不可变数据的类,其一部分是 是一个更通用的结构,可以有行为和状态。

何时使用记录,何时使用类?

如果需要的是一个不可变的数据结构来存储数据,并且不需要对属性进行修改(简单地看成是一个携带信息的对象)。另一方面,如果您需要一个具有独特逻辑和特定方法的更通用的结构、一种面向对象范例的方法、应用设计模式或使用 JPA 或 Hibernate 等,那么您应该使用类。

Extra: Record con atributos mutables

Consideremos el siguiente ejemplo, se tienen dos records Product con los atributos name y price, y Cart con un solo atributo products del tipo ArrayList y algunos métodos para obtener la cantidad de productos y el total del carrito.

package org.jordi.example;

public record Product(String name, double price) { }
Copy after login
package org.jordi.example;

import java.util.ArrayList;
import java.util.List;

public record Cart(List<Product> products) {

    public Cart() {
        this(new ArrayList<>());
    }

    public int getQuantity() {
        return this.products.size();
    }

    public double getTotal() {
        return this.products.stream().mapToDouble(Product::price).sum();
    }
}
Copy after login

La cuestión en este caso es que cada uno de los record es inmutable por sí mismo, pero en el caso del record Cart al tener un atributo del tipo ArrayList<> y dado que por naturaleza un ArrayList es mutable, se puede modificar el contenido de la lista una vez que se crea una instancia del record Cart.

package org.jordi.example;

public class Main {
    public static void main(String[] args) {
        Product water = new Product("Water", 15);
        Product milk = new Product("Milk", 22);

        Cart cart = new Cart();
        cart.products().add(water);
        cart.products().add(milk);
        System.out.println("Price: " + cart.getTotal());

        cart.products().clear();
        System.out.println("Quantity: " + cart.getQuantity());
        System.out.println("Price: " + cart.getTotal());
    }
}
Copy after login

El código anterior compila sin problemas, ya que solo se está modificando el contenido de la lista, pero no se está modificando el atributo products en sí. Este solo es un ejemplo para un caso particular, que probablemente no sea necesario, pero es bueno saber que esto se puede realizar.

The above is the detailed content of Records vs Classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles