Java 类名冲突的折衷办法

考虑一个java项目的加载项

import org.carrot2.core.Document;

import org.bson.Document;

显然不能同时加载以上两个包,因为它们的类名都是Document

但假如程序中两个Document类都需要用到的话,可以作如下折衷

加载项:

import org.carrot2.core.Document;

TODO 代码(以 mongoDB-driver 为例):

MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection(DATABASE_NAME);

 

也就是只加载一个包,而另一个包的类在使用时写上全名。

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud

This site uses Akismet to reduce spam. Learn how your comment data is processed.