JAVA使用反射机制获取Record类型的类对象判断是否是Record类型并取出所有的Component组件------JAVA

package com.example.demo;

import org.junit.Test;

import java.lang.reflect.RecordComponent;
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 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);
    }

    @Test
    public void Test07(){
        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.address().city());
        System.out.println(customer.phoneNumber().number());
        Class customerClass = customer.getClass();
        System.out.println(customerClass.isRecord());
        RecordComponent[] recordComponents = customerClass.getRecordComponents();
        for (RecordComponent recordComponent : recordComponents){
            System.out.println(recordComponent);
        }
    }
}
 
   
package com.example.demo;

import org.junit.Test;

import java.lang.reflect.RecordComponent;
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 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);
    }

    @Test
    public void Test07(){
        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.address().city());
        System.out.println(customer.phoneNumber().number());
        Class customerClass = customer.getClass();
        System.out.println(customerClass.isRecord());
        RecordComponent[] recordComponents = customerClass.getRecordComponents();
        for (RecordComponent recordComponent : recordComponents){
            System.out.println(recordComponent);
        }
    }
}
 
   
  
package com.example.demo;

public record Address(String city,String address,String zipcode) {
}
package com.example.demo;

public record Address(String city,String address,String zipcode) {
}
package com.example.demo;

public record Customer(String id,String name,PhoneNumber phoneNumber,Address address) {
}
package com.example.demo;

public record Customer(String id,String name,PhoneNumber phoneNumber,Address address) {
}
package com.example.demo;

public record PhoneNumber(String areaCode,String number) {
}
package com.example.demo;

public record PhoneNumber(String areaCode,String number) {
}

你可能感兴趣的:(JAVA,java,maven,xml,list,junit,后端,mybatis)