小编ilu*_*uxa的帖子

处理一组重写方法取决于它是任意的还是交替的

我有这段代码,我想知道第一个输入和第二个输入的执行时间之间存在差异的原因。我认为它应该花费相同的时间,因为我正在调用相同的方法,但它在两个对象中不执行任何操作。但是 input1 (这是 2 个实例的交替)在我的计算机上花费了 5 秒。input2(这是两个实例之间的任意选择)在我的计算机上花费了 17 秒。

public class Program {


    private static final Runnable FIRST_INSTANCE = () -> {};
    private static final Runnable SECOND_INSTANCE = () -> {};

    private static Runnable[] input1() {
        Runnable[] array = new Runnable[10000];
        for (int i = 0; i < array.length; i++) {
            array[i] = i % 2 == 0 ? FIRST_INSTANCE : SECOND_INSTANCE;
        }
        return array;
    }


    private static Runnable[] input2() {
        Random rnd = new Random(0);
        Runnable[] array = new …
Run Code Online (Sandbox Code Playgroud)

java algorithm lambda overriding time-complexity

6
推荐指数
1
解决办法
160
查看次数

带有Guice注入字段的Java构造函数以及非注入字段

我有一个类,它有一个构造函数,其中所有参数都由GUICE注入.

Public class Order {

    private final ClassOne classOneObj;
    private final ClassTwo classTwoObj;

    @Inject
    public order(ClassOne classOneObj, ClassTwo classTwoObj){
    this.classOneObj = classOneObj;
    this.classTwoObj = classTwoObj;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想再添加一个无法注入的字段(例如,int status)变量.

首先使用所有注入的参数创建对象然后设置无法使用setter方法注入的新字段是一个好习惯吗?

我提出了另一种方法,我创建了一个工厂类,如下所示:

public class OrderFactory {

    private final ClassOne classOneObj;
    private final ClassTwo classTwoObj;

    @Inject
    public order(ClassOne classOneObj, ClassTwo classTwoObj){
    this.classOneObj = classOneObj;
    this.classTwoObj = classTwoObj;
    }

   //getter methods for all the above variables

    public  ClassOne getclassOneObj(){
          return classOneObj;
    }
    ....

    public Order createOrder(int status) {
        return new Order(status, classOneObj, classTwoObj); …
Run Code Online (Sandbox Code Playgroud)

java constructor guice code-injection

5
推荐指数
1
解决办法
1255
查看次数