小编Art*_*tur的帖子

工厂方法如何返回接口和抽象类的实例?

ExecutorService和Service是接口,因此只有抽象方法,这意味着它们的方法没有实现.我们再怎么称呼,例如future.get(),es.submit()es.shutdown()方法的接口类型的参考?例如,我们为什么要这样做呢?

Future f = ...
f.get();
Run Code Online (Sandbox Code Playgroud)

这是一个更具体的例子:

import java.util.concurrent.*;
class Factorial implements Callable<Long> {
    long n;
    public Factorial(long n) {
        this.n = n;
    }

    public Long call() throws Exception {
        if (n <= 0) {
            throw new Exception("for finding factorial, N should be > 0");
        }
        long fact = 1;
        for(long longVal = 1; longVal <= n; longVal++) {
            fact *= longVal;
        }
        return fact;
    }
}

class CallableTest {
    public static void …
Run Code Online (Sandbox Code Playgroud)

java factory interface abstract

2
推荐指数
1
解决办法
1880
查看次数

标签 统计

abstract ×1

factory ×1

interface ×1

java ×1