简体   繁体   English

我如何创建具有与类相同的参数的泛型构造函数

[英]How can i create a constructor with generics with same class as argument

I have a class Expression: 我有一个类Expression:

public class Expression < E extends Number, V extends Number >
{
   public Expression(E lV, OPERATION operation, V rV) {
   }

   public Expression(Expression< E, V > lE, OPERATION operation, Expression< E, V > rE) {
   }
}

Expression.java compile without errors. Expression.java编译没有错误。

This is my main class code. 这是我的主要课程代码。

public static void main(String[] args)
{
        // Line 1. 
    refactored.Expression< ?, ? > ex1 = new refactored.Expression< Double, Float >(10d, OPERATION.PLUS, 10f);

        // Line 2.
    refactored.Expression< ?, ? > ex2 = new refactored.Expression< Double, Float >(-3d, OPERATION.MUL, 1f);

        // Line 3.
    refactored.Expression< ?, ? > ex3 = new refactored.Expression< refactored.Expression< Double, Float >, refactored.Expression< Double, Float > >(ex1, OPERATION.MINUS, ex2);
}

The Line 3 doesn't compile, it says: 3号线无法编译,它说:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Expression<Expression<Double,Float>,Expression<Double,Float>>(Expression<capture#1-of ?,capture#2-of ?>, OPERATION, Expression<capture#3-of ?,capture#4-of ?>) is undefined
Bound mismatch: The type Expression<Double,Float> is not a valid substitute for the bounded parameter <E extends Number> of the type Expression<E,V>
Bound mismatch: The type Expression<Double,Float> is not a valid substitute for the bounded parameter <V extends Number> of the type Expression<E,V>

What's wrong with it? 它出什么问题了?

Expression does not extend number so it is not a legal type parameter. 表达式不扩展数字,因此不是合法的类型参数。

edit - to address the comment 编辑-解决评论
I am not sure why you need the generic solution for this... 我不确定为什么您需要通用的解决方案...
you can have something like 你可以有类似的东西

interface Expression {
  Number compute();
}

class AtomicExpression implements Expression {
  private final Number number;
  AtomicExpression(Number number) {
    this.number = number;
  }
  public Number compute() {
    return number;
  }
}

class BinaryExpression implements Expression {
  private final Expression expr1;
  private final Expression expr2;
  private final Operator op;
  AtomicExpression(Expression expr1, Expression expr1. Operator op) {
    this.expr1 = expr1;
    this.expr2 = expr2;
    this.op = op;
  }
  public Number compute() {
    return op(expr1.compute(), expr2.compute());
  }
}

refactored.Expression< ?, ? 重构.Expression <?,? > ex3 = new refactored.Expression < Double, Float>(ex1, OPERATION.MINUS, ex2); > ex3 =新重构的.Expression <Double,Float>(ex1,OPERATION.MINUS,ex2);

This should work. 这应该工作。

It is obvious from errors you're getting that Expression<?, ?> is not a subclass of Number. 从错误中可以明显看出, Expression<?, ?>不是Number的子类。 If you really want to accept Expression in your constructor then you need to remove the upper bonded parameter (Number) from generics. 如果您确实想在构造函数中接受Expression,则需要从泛型中删除上限的绑定参数 (数字)。

Something like this should work for you: 这样的事情应该为您工作:

public class Expression <E, V> {
    public Expression(E lV, OPERATION operation, V rV) {}
    public Expression(Expression< E, V > lE, OPERATION operation, Expression< E, V > rE) {}
}

Update: This should work then: 更新:这应该工作然后:

Expression< Double, Float > ex1 = new Expression< Double, Float >(10d, OPERATION.PLUS, 10f);

Expression< Double, Float > ex2 = new Expression< Double, Float >(-3d, OPERATION.MUL, 1f);

Expression< ?, ? > ex3 = new Expression
 < Expression< Double, Float >, Expression< Double, Float > >(ex1, OPERATION.MINUS, ex2);

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

相关问题 如何使用 generics 创建 AutoValue class? - How can I create a AutoValue class with generics? Java泛型 - 类 <?> 构造函数参数问题 - Java generics - Class<?> constructor argument issue 如何在不调用此类的构造函数的情况下创建类的实例? - How can I create an instance of class without invoking constructor of this class? 如何检查接口和 class 的构造函数中的参数是否相同? - How can I check to see if the parameters in a constructor for an interface and the class is the same? 如何为XML解析创建泛型? - How can I create Generics for XML Parsing? 如何在 Java 中获取带有 Generics 的反射构造函数? - How can I get the reflect constructor with Generics in Java? Java - 如何创建一个 class 可以处理特定 [restricted] 类型的 Generics 对象? - Java - How can I create a class that can process objects of Generics of a specific [restricted] type? 如何在构造函数中传递参数? - How can I pass argument in the constructor? 我们可以通过传递类类型作为参数来使用 Java 泛型创建不同的类实例吗 - Can we create different class instance using Java generics by passing class type as argument 当我需要使用带有参数的构造函数时,如何使用Rhino子类化(扩展)Java类? - How can I subclass (extend) a Java class using Rhino when I need to use a constructor that takes an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM