2
的背景是:使用類名::新(Java 8)來生成一個類的實例失敗
- IDE:的IntelliJ
- 使用龍目島@Data @AllArgsConstructor註釋我的模型類
編譯沒問題,但運行下列情況失敗。
/** This is the model class */ @Data @AllArgsConstructor class Message { public Long id; public String name; } /** This is a test case */ @Test public void testConstructRef() { BiFunction<Long, String, Message> constructFunc = Message::new; Message msg = constructFunc.apply(1L, "this is a message"); assertEquals(1L, (long) msg.getId()); assertEquals("this is a message", msg.getName()); }
從IDE中,Message
構造&的getter/setter都在那裏。 編譯正常,但運行失敗。
所以我的問題是:
- 它是一個的IntelliJ發行或龍目島問題或Java 8的問題?
- 以前有人遇到過這個嗎?如何解決它,如果我不得不使用龍目島? (如果我手動編寫的構造/吸氣/設定器,它可以成功地運行。)
順便說一句,運行的錯誤是:
Error:(40, 63) java: incompatible types: invalid constructor reference
constructor Message in class java8.methodref.TestDemo.Message cannot be applied to given types
required: no arguments
found: java.lang.Long,java.lang.String
reason: actual and formal argument lists differ in length
Error:(43, 40) java: cannot find symbol
symbol: method getId()
location: variable msg of type java8.methodref.TestDemo.Message
Error:(44, 50) java: cannot find symbol
symbol: method getName()
location: variable msg of type java8.methodref.TestDemo.Message
什麼是錯誤? –
錯誤:(40,63)java:不兼容的類型:無效的構造函數引用 構造函數類java8.methodref.TestDemo.Message中的消息不能應用於給定類型 必需:無參數 found:java.lang.Long,java。 lang.String 原因:實際的和正式的參數列表的長度不同 錯誤:(43,40)的java:無法找到符號 符號:方法的getId() 位置:類型的可變MSG java8.methodref.TestDemo.Message 錯誤:(44,50)java:找不到符號 symbol:method getName() location:java8.methodref.TestDemo.Message類型的變量msg – JasonS
對我來說完美無瑕。 「IntelliJ IDEA的2016.3 構建#IU-163.7743.44,建於2016年11月17日 JRE:1.8.0_112釋放-408-B2 AMD64 JVM:OpenJDK的64位服務器VM通過JetBrains的SRO 」 –