Java 8 새로운 기능 메소드 참조
참조의 경우, 우리는 일반적으로 객체에서이를 사용하며 객체 참조의 특성은 다른 참조 객체가 동일한 컨텐츠에서 작동 할 수 있다는 것입니다!
Java 8 메소드 참조는 네 가지 형식을 정의합니다.
정적 메소드 참조 예제
/*** 정적 메소드 참조* @param <p> 매개 변수 유형의 참조 메소드* @param <r> 참조 메소드의 반환 유형* / @functionalinterfaceinterface funstaticref <p, r> {public r trantest (p p);} public static void main (strings) { /** 정적 메소드 참조 : public string value value of ()는 ()가 말하면 () FunstaticRef#trantest 메소드*/ funstaticRef <Integer, String> funstaticRef = String :: ValueOf; 문자열 str = funstaticref.trantest (10000); System.out.println (str.replaceall ( "0", "9");}
객체 메소드 참조 예제
/*** 일반 메소드 참조* @param <r> 참조 메소드 반환 유형*/ @functionalInterfaceinterface Instanref <r> {public r 대문자 ();} public static void main (string [] args) {/** 일반적인 방법의 참조 : public String toupperCase ()**/string str2 = "i see you"; Instanref <string> instanref = str2 :: touppercase; System.out.println (instanref.uppercase ());} 특정 유형 메소드 참조의 예
특정 방법의 인용은 이해하기 어렵다. 일반 방법을 말하지만 참조 방법은 다음과 같습니다. ClassName :: MethodName
/** * 특정 방법에 대한 참조 * @param <p> * / @functionalInterfaceinterface 특이 적 Methodref <p> {public int compare (p p1, p p2);} public static void main (string [] args) { / * * 공개 int 비교 (string an anthstring) *에 대한 참조는 더 이상 정의되기 전에 정의 될 필요가 없지만, 방법을 언급 할 필요는 없지만 매개 변수! */ sultmethodref <string> sultmethodref = string :: compareto; System.out.println (특정 methodref.compare ( "a", "b")); ConstructorRef <book> constructorref = book :: new; Book Book = ConstructorRef.createObject ( "Java", 100.25); System.out.println (Book);}생성자 참조의 예
클래스 북 {개인 문자열 제목; 개인 이중 가격; public book () {} public book (문자열 제목, 이중 가격) {this.price = price; this.title = 제목; } @override public String toString () {return "book {" + "title = ' + title +'/' +", price = " + price +'} '; }} public static void main (string [] args) { / * * constructorref <book> constructorref = book :: new; Book Book = ConstructorRef.createObject ( "Java", 100.25); System.out.println (Book);}일반적으로 Java 8의 새로운 기능은 현재 사용중인 프로젝트에서 사용되지 않았지만 학습 후에는 Java 8 의이 새로운 기능 코드를 볼 수 없으며 무엇이 잘못되었는지 모릅니다!
읽어 주셔서 감사합니다. 도움이되기를 바랍니다. 이 사이트를 지원 해주셔서 감사합니다!