JAVA的Record类型的instanceof和自动判空机制------JAVA

package com.example.demo;

import org.junit.Test;

import java.util.ArrayList;

/**
 * Unit test for simple App.
 */
public class StudentTest {
    @Test
    public void Test01(){
//        创建Record对象
//        这里也会先执行紧凑的构造方法
        Student Rose = new Student(1001,"Rose","abc",15);
        System.out.println(Rose.toString());
        Integer age = Rose.age();
        System.out.println(age);
        String name = Rose.name();
        System.out.println(name);
        String email = Rose.email();
        System.out.println(email);
        System.out.println("哈哈");
        System.out.println(Rose.concat());
        System.out.println(Student.emailToUpperCase("Dasd"));
        Student jack = new Student(2001, "Jack");
        System.out.println(jack);
    }
    @Test
    public void Test02(){
        ProductRecord record = new ProductRecord(1,"手机",200);
        record.print();
        ArrayList<Object> objects = new ArrayList<>();
        System.out.println(objects.get(1));
    }
    @Test
    public void Test03(){
//        定义java的Record
        record SaleRecord(String saleId,String productName,Double money){}
        SaleRecord saleRecord = new SaleRecord("S001","显示器",3000.1);
        System.out.println(saleRecord);
    }

    @Test
    public void Test04() {
        Address address = new Address("北京","大兴区凉水河二街八号10栋3层","100176");
        PhoneNumber phoneNumber = new PhoneNumber("010","400-8080-105");
        Customer customer = new Customer("c101","Jack",phoneNumber,address);
        System.out.println(customer);
    }
    @Test
    public void Test05(){
        Person person = new Person("Jack",10);
        SomeService someService = new SomeService();
        boolean eligible = someService.isEligible(person);
        System.out.println(eligible);
    }
    @Test
    public void Test06(){
        SomeService someService = new SomeService();
        boolean eligible = someService.isEligible(null);
        System.out.println(eligible);
    }
}
package com.example.demo;

import org.junit.Test;

import java.util.ArrayList;

/**
 * Unit test for simple App.
 */
public class StudentTest {
    @Test
    public void Test01(){
//        创建Record对象
//        这里也会先执行紧凑的构造方法
        Student Rose = new Student(1001,"Rose","abc",15);
        System.out.println(Rose.toString());
        Integer age = Rose.age();
        System.out.println(age);
        String name = Rose.name();
        System.out.println(name);
        String email = Rose.email();
        System.out.println(email);
        System.out.println("哈哈");
        System.out.println(Rose.concat());
        System.out.println(Student.emailToUpperCase("Dasd"));
        Student jack = new Student(2001, "Jack");
        System.out.println(jack);
    }
    @Test
    public void Test02(){
        ProductRecord record = new ProductRecord(1,"手机",200);
        record.print();
        ArrayList<Object> objects = new ArrayList<>();
        System.out.println(objects.get(1));
    }
    @Test
    public void Test03(){
//        定义java的Record
        record SaleRecord(String saleId,String productName,Double money){}
        SaleRecord saleRecord = new SaleRecord("S001","显示器",3000.1);
        System.out.println(saleRecord);
    }

    @Test
    public void Test04() {
        Address address = new Address("北京","大兴区凉水河二街八号10栋3层","100176");
        PhoneNumber phoneNumber = new PhoneNumber("010","400-8080-105");
        Customer customer = new Customer("c101","Jack",phoneNumber,address);
        System.out.println(customer);
    }
    @Test
    public void Test05(){
        Person person = new Person("Jack",10);
        SomeService someService = new SomeService();
        boolean eligible = someService.isEligible(person);
        System.out.println(eligible);
    }
    @Test
    public void Test06(){
        SomeService someService = new SomeService();
        boolean eligible = someService.isEligible(null);
        System.out.println(eligible);
    }
}
package com.example.demo;

public class SomeService {
//    定义一个业务方法,判断成年
    public boolean isEligible(Object object){
//        record可以自动判断出这里是个null
        if( object instanceof Person(String name,Integer age)){
            return age >= 18;
        }
        return false;
    }
}
package com.example.demo;

public class SomeService {
//    定义一个业务方法,判断成年
    public boolean isEligible(Object object){
//        record可以自动判断出这里是个null
        if( object instanceof Person(String name,Integer age)){
            return age >= 18;
        }
        return false;
    }
}
package com.example.demo;

public record Person(String name,Integer age) {
}
package com.example.demo;

public record Person(String name,Integer age) {
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>demo</description>
	<properties>
		<java.version>21</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>21</source>
					<target>21</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>3.2.2</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
       <java.version>21</java.version>
    </properties>
    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
       </dependency>
       <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>
    </dependencies>

    <build>
       <plugins>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             <configuration>
                <excludes>
                   <exclude>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                   </exclude>
                </excludes>
             </configuration>
          </plugin>
          <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <configuration>
                <source>21</source>
                <target>21</target>
             </configuration>
          </plugin>
       </plugins>
    </build>

</project>
  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值