关于java泛型的小问题?

大家好,请看演示代码及问题:

先定义一个类:

public class Arr<S> {

private S entity;

private IService<S> service;

public Arr(S entity,IService<S> service){

this.entity = entity;

this.service = service;

}

}

然后创建一个List,来存放上面类的多个实例:

ArrayList<Arr<?>> list = new ArrayList<>();

现在开始创建实例,并添加到list中:

@Autowired

private IUserService userService;

@Autowired

private IDogService dogService;

@Autowired

private ICarService carService;

Arr<User> a = new Arr<>(new User(),userService);

Arr<Dog> b = new Arr<>(new Dog(),dogService);

Arr<Car> c = new Arr<>(new Car(),carService);

list.add(a);

list.add(b);

list.add(c);

最后,也是问题所在,我开始遍历list:

for (Arr<?> item :list) {

//我的问题就是:怎么将注释部分的代码改成泛型形式的代码。

//LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

//wrapper.eq(User::getId,"1");

//User user = item.service.getOne(wrapper);

//System.out.println(user);

}

我想在上面遍历代码中,每次取出item的entity属性的类型,然后通过该类型创建Mybatis plus的
LambdaQueryWrapper,但是我怎么能获取到这个属性对应的类型呢?也就是上面注释的User,只不过每次遍历分别是Uesr、Dog、Car。谢谢!~


回答:

使用如下方法:

// 获取类

item.getClass();

// 获取类名

item.getClass().getName();


回答:

补充 如果只是构造查询条件,其实 mp有多种方式,你这种情况,建议这种玩法

 for (Arr<?> item : list) {

Class<?> entityClass = item.getEntity().getClass();

if (entityClass.equals(ChenXiModelSyncCount.class)) {

ChenXiModelSyncCount chenXiModelSyncCount = (ChenXiModelSyncCount) item.getEntity();

IChenXiService chenXiService= (IChenXiService) item.getService();

//重点在这里

LambdaQueryChainWrapper<ChenXiModelSyncCount> lambdaQueryChainWrapper = chenXiService.lambdaQuery();

List<ChenXiModelSyncCount> list1 = lambdaQueryChainWrapper.list();

ChenXiModelSyncCount one = lambdaQueryChainWrapper.eq(ChenXiModelSyncCount::getId, 1).one();

// ....

//..

} else if (entityClass.equals(BsDirectoryTreeDo.class)) {

BsDirectoryTreeDo bsDirectoryTreeDo = (BsDirectoryTreeDo) item.getEntity();

IBsDirectoryTreeService bsDirectoryTreeService= (IBsDirectoryTreeService) item.getService();

//...

}

}

@Service

public class ArrService {

@Autowired

IChenXiService chenXiService;

@Autowired

IBsDirectoryTreeService bsDirectoryTreeService;

public void test() {

ArrayList<Arr<?>> list = new ArrayList<>();

Arr<ChenXiModelSyncCount> a = new Arr<>(new ChenXiModelSyncCount(), chenXiService);

Arr<BsDirectoryTreeDo> c = new Arr<>(new BsDirectoryTreeDo(), bsDirectoryTreeService);

list.add(a);

list.add(c);

for (Arr<?> item : list) {

Class<?> entityClass = item.getEntity().getClass();

if (entityClass.equals(ChenXiModelSyncCount.class)) {

ChenXiModelSyncCount chenXiModelSyncCount = (ChenXiModelSyncCount) item.getEntity();

IChenXiService chenXiService= (IChenXiService) item.getService();

//..

} else if (entityClass.equals(BsDirectoryTreeDo.class)) {

BsDirectoryTreeDo bsDirectoryTreeDo = (BsDirectoryTreeDo) item.getEntity();

IBsDirectoryTreeService bsDirectoryTreeService= (IBsDirectoryTreeService) item.getService();

//...

}

}

}

}


回答:

public class App {

public static void main(String[] args) throws Exception {

List<Parent> list = Arrays.asList(new User(new UserService()), new Dog(new DogService()));

list.forEach(Parent::fun);

}

interface Service {

void query();

}

static class UserService implements Service {

@Override

public void query() {

System.out.println("User query");

}

}

static class DogService implements Service {

@Override

public void query() {

System.out.println("Dog query");

}

}

static abstract class Parent {

abstract void fun();

}

static class User extends Parent {

private Service service;

public User(Service service) {

this.service = service;

}

@Override

void fun() {

System.out.println("this is user");

service.query();

}

}

static class Dog extends Parent {

private Service service;

public Dog(Service service) {

this.service = service;

}

@Override

void fun() {

System.out.println("this is dog");

service.query();

}

}

}


回答:

思路不对,泛型不是这么用的。
除非 Arr<?> 有公共的方法,或者 UserDogCar 继承自某个共同的父类
否则for循环List<Arr<?>>没有任何意义 和for循环List<Object>没区别。
你应该先想好自己想做什么,或者明确一下具体的使用场景

以上是 关于java泛型的小问题? 的全部内容, 来源链接: utcz.com/p/944577.html

回到顶部