我想创建一个简单的通用方法来计算应用过滤器后的数字.
static int count(Collection < ? extends Number > numbers, Predicate < ? extends Number > predicate) {
return numbers.stream().filter(predicate).count();
}
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
不兼容的类型:谓词不能转换为
Predicate<? super CAP#2>CAP#1,CAP#2是新的类型变量:CAP#1从捕获数量扩展数量?扩展数量
CAP#2从捕获数量扩展数量?扩展数量
假设我有一个名为CommandLineOperation. 此类访问 API 资源。因此我定义了一个 type 的实例成员APIAccessor。
class CommandLineOperation {
APIAccessor apiAccessor;
void create() {
apiAccessor = new APIAccessor(email,password);
//do work for creation
}
void update() {
apiAccessor = new APIAccessor(email,password);
//do work for update
}
}
class APIAccessor {
String email;
String password;
APIAccessor(email,password) {
this.email = email;
this.password = password;
}
}
Run Code Online (Sandbox Code Playgroud)
CommandLine 中的操作不常见,APIAccessor在每个操作下实例化或使用CommandLineOperation类的构造函数创建一次是更好的方法。例如
CommandLineOperation(String email,String password) {
this.apiAccessor = new APIAccessor(email,password);
}
Run Code Online (Sandbox Code Playgroud)
请让我知道或建议好的编码设计模式。或者推荐任何参考书,以便我可以根据分析改进我的编码标准。提前致谢。
我上课A。此类包含两个方法,比如say method1()和method2()。现在,我还有另外两个名为B和的类C。两者都将包含相同的Aclass 实例。
现在,我想以“ B class can only callmethod1()and my otherC”类可以调用的方式来限制访问method2()。设计方案如下
class A {
void method1(){/*-------------*/}
void method2(){/*-------------*/}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建的实例A并与我的B和C类共享
class B {
A ob;
public B(A ob) {
this.ob=ob;
}
public void process() {
ob.method1(); //only call method1()
ob.method2(); //can't access it.
}
}
class C {
A ob;
public C(A ob) {
this.ob=ob;
}
public void …Run Code Online (Sandbox Code Playgroud) 我是Thread的新手.我创建了两个名为A和B的类,如下所示 -
public class A {
private B b;
public void setB(B b) {
this.b = b;
}
synchronized void foo() {
b.foo();
System.out.println("Hi A");
}
}
public class B {
private A a;
public void setA(A a) {
this.a = a;
}
synchronized void foo() {
a.foo();
System.out.println("Hi B");
}
}
Run Code Online (Sandbox Code Playgroud)
现在我创建了另外两个实现Runnable接口的类.
public class AThread implements Runnable{
private A a;
public AThread(A a){
this.a = a;
}
@Override
public void run() {
a.foo();
}
}
public class BThread implements …Run Code Online (Sandbox Code Playgroud) 我将创建一个通用类,首先我想说一下我的要求。我有不同的类,例如 A、B 等。我将基于 json 对象创建一个类的实例。这个 json 对象将从文件中读取。该文件可能包含等效的 json 对象。基于它,我将使用 GSON 创建该类的实例。现在我面临一个错误,即incompatible types: T#1 cannot be converted to T#2
这是我的代码示例
public class JsonLoader<T> {
private final Gson gson = new Gson();
private final T content;
public <T> JsonLoader(Class<T> clazz, String filePath) throws IllegalFileException {
if (filePath.isEmpty() || filePath == null) {
throw new IllegalFileException("IllegalFileException: source file must required.");
}
try (Reader reader = new FileReader(filePath)) {
T content= gson.fromJson(reader, clazz);
this.content = content;
} catch (IOException e) {
throw new …Run Code Online (Sandbox Code Playgroud) 我有一个工厂类,假设通过调用方法ApppleFactory创建一个新实例.只有一个共享实例.ApplegetApple()ApppleFactory
class ApppleFactory {
private static Apple apple = null;
private ApppleFactory() {
}
public static Apple getApple() {
apple = new Apple();
//do some other work on this apple instance
return apple; //finally return it
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果多个线程访问它getApple()并使用这个可变数据, apple则可能存在不安全的可能性.如何克服这个问题?