>  기사  >  Java  >  java8의 새로운 java.util.function.*pojo 반영 방법 이해(코드 포함)

java8의 새로운 java.util.function.*pojo 반영 방법 이해(코드 포함)

奋力向前
奋力向前앞으로
2021-09-14 11:57:462622검색

이전 글 "EOS 블록체인 자몽 지갑 프런트엔드 플러그인 분산 개발(공유)에 대한 간략한 분석"에서 블록체인 내 EOS 지갑 프런트엔드 플러그인 분산 개발에 대해 알아보았습니다. 다음 글에서는 새로운 java.util.function.*pojo 리플렉션 메소드를 소개하겠습니다.

java8의 새로운 java.util.function.*pojo 반영 방법 이해(코드 포함)

코드로 이동하여 예제 보기

공통 POJO 작성

public class City {

    private String name;
    private String code;

    public City() {
    }

    public City(String name, String code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}

전통적인 방식

// Use a constructor with parameters to create a City
City sf = new City("San Francisco", "SF");
// Use a default constructor with no parameters to create a City
City la = new City();
// Set the members using setters
la.setName("Los Angeles");
la.setCode("LA");

새로운 getter 액세스 방법

// Use the City's method references and assign them to functions
Function<City, String> getNameFunction = City::getName;
Function<City, String> getCodeFunction = City::getCode;

System.out.println("The code for "

        + getNameFunction.apply(sf)
        + " is "
        + getCodeFunction.apply(sf));

-> The code for San Francisco is SF

새로운 Setter 액세스 방법

// Use the City&#39;s method references and assign them to biconsumers
BiConsumer<City, String> setNameBiConsumer = City::setName;
BiConsumer<City, String> setCodeBiConsumer = City::setCode;
City ny = new City();
setNameBiConsumer.accept(ny, "New York");
setCodeBiConsumer.accept(ny, "NY");

액세스 생성자 새 인스턴스 만들기

// Use the City&#39;s constructor method reference to create
// a default constructor reference.
Supplier<City> defaultConstructor = City::new;

City sd = defaultConstructor.get();
sd.setName("San Diego");
sd.setCode("SD");

매개변수가 있는 생성자

// Use the City&#39;s constructor method reference to create
// a two-parameter constructor reference.
BiFunction<String, String, City> twoParameterConstructor = City::new;

City dc = twoParameterConstructor.apply("Washington, D. C.", "DC");

추천 학습: java 비디오 튜토리얼

위 내용은 java8의 새로운 java.util.function.*pojo 반영 방법 이해(코드 포함)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 chuchur.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제