简体   繁体   English

错误:不能对构造函数使用带有显式类型参数的原始构造函数引用

[英]Error: cannot use raw constructor reference with explicit type parameters for constructor

I have the following pieces of code:我有以下几段代码:

Main.java主程序

public class Main {
  public static void main(String[] args) {
     TestProducer c = Test::<String>new;
     System.out.println(c.produce("Point"));
  }
}

Test.java测试.java

public class Test<N extends CharSequence> {
  private CharSequence name;

  public Test(N k) {
    name = k;
  }

  public String toString() {
    return name.toString();
  }
}

TestProducer.java测试生产者.java

@FunctionalInterface
public interface TestProducer {
  public <N extends CharSequence> Test<N> produce(N str);
}

When I compile the code, it produces the following error:当我编译代码时,它会产生以下错误:

OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
> javac -classpath .:/run_dir/junit-4.12.jar -d . Main.java Test.java TestProducer.java
Main.java:3: error: invalid constructor reference
     TestProducer c = Test::<String>new;
                      ^
  cannot use raw constructor reference with explicit type parameters for constructor
1 error
compiler exit status 1

I have the following questions:我有以下问题:

  1. What is the "raw constructor reference" here ("explicit type parameters" here is <String> )?这里的“原始构造函数引用”是什么(这里的“显式类型参数”是<String> )?
  2. Why can't you "use raw constructor reference with explicit type parameters for constructor" (reasoning/why it's not a good idea)?为什么不能“对构造函数使用带有显式类型参数的原始构造函数引用”(推理/为什么这不是一个好主意)?
  3. How can I fix this error (create a TestProducer that only accepts String s)?我该如何解决这个错误(创建一个只接受String的 TestProducer)?

EDIT:编辑:

With regards to avoiding the error, here's what I found:关于避免错误,这是我发现的:
I can modify TestProducer to the following:我可以将TestProducer修改为以下内容:

@FunctionalInterface
public interface TestProducer<N extends CharSequence>
{
  public Test<N> produce(N str);
}

and then change Main.java like so:然后像这样更改Main.java

public class Main {
  public static void main(String[] args) {
     TestProducer<String> c = Test::new;
     System.out.println(c.produce("Point"));
  }
}

By doing this, c::produce will only accept String s, which fits my intent for question 3. Questions 1 and 2 are still unclear.通过这样做, c::produce将只接受String s,这符合我对问题 3 的意图。问题 1 和问题 2 仍然不清楚。

You define your TestProducer interface not to be generic itself but to have a generic method:您将TestProducer接口定义为不是泛型本身,而是具有泛型方法:

Test<N> produce(N str);

Then you try to bind a specific type in the assignment:然后你尝试在赋值中绑定一个特定的类型:

TestProducer c = Test::<String>new;

But it's completely valid for me now to say this:但我现在这样说是完全有效的:

c.produce(new StringBuilder())

which must return a Test<StringBuilder> , not a Test<String> .它必须返回一个Test<StringBuilder> ,而不是一个Test<String>

You should probably add N as a type parameter on your interface.您可能应该在界面上添加N作为类型参数。

  1. Remove the type witnesses from around your constructor.从构造函数周围删除类型见证。 That won't work even in Java 8 .即使在 Java 8 中也行不通。

     TestProducer c = Test::new; System.out.println(c.produce("Point"));
  2. You don't need to provide type witnesses anyway.无论如何,您不需要提供类型见证。 TestProducer is providing a strongly-enforced contract based on the produce method definition, and any producer which doesn't satisfy that definition can't be used. TestProducer提供基于produce方法定义的强执行契约,任何不满足该定义的生产者都不能使用。

  3. If you wanted a TestProducer which only accepted String s, then you'd need to change your bound to <N extends String> .如果您想要一个接受StringTestProducer ,那么您需要将绑定更改为<N extends String>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java错误默认构造函数无法处理隐式超级构造函数引发的异常类型SQLException。 必须定义一个显式构造函数 - Java error Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor 默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor 默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor 给定类型的构造函数错误,无法应用 - Constructor error with given type, cannot be applied 使用带有 generics 的构造函数引用 - Use constructor reference with generics 为什么C ++和Java中的构造函数调用需要显式类型参数? - Why do constructor calls in C++ and Java require explicit type parameters? 忽略默认构造函数,并在Java中将构造函数与参数一起使用 - Ignore default constructor and use constructor with parameters in Java 必须定义显式构造函数Error Java - Must define an explicit constructor Error Java 在Java泛型中的方法/构造函数之前使用类型参数 - Use of type-parameters before a method/constructor in Java Generics 如何使用一种方法来引用在构造函数中创建的数据类型(数组) - How to use a method to reference a data type(array) that is created in a constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM