gpt4 book ai didi

java - 如何制作泛型的构造函数

转载 作者:行者123 更新时间:2023-11-29 20:13:40 25 4
gpt4 key购买 nike

给定下面的代码,我怎样才能构造一个泛型类型的构造函数,以便它可以接受传递给它的任何类

代码:

ServiceCtrl(Context ctx, Class<SPPService> sppServiceClass) {
this.mCtx = ctx;
this.mClass = sppServiceClass;
}

public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

最佳答案

public class ServiceCtrl<E extends Object> {
private Class<E> mClass;

ServiceCtrl(Context ctx, Class<E> sppServiceClass) {
this.mCtx = ctx;
this.mClass = sppServiceClass;
}

public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}

关于java - 如何制作泛型的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34391629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com