[英]MongoDB Java-Driver: Cannot extend MongoDatabase
i'm coding on a MongoDB java application. 我在MongoDB Java应用程序上编码。
I want to create a Collection of the objects (Type MongoDatabase) of every database from the mongoClient. 我想从mongoClient创建每个数据库的对象的集合(类型MongoDatabase)。 Check
校验
Next step: i want to extend the Type MongoDatabase to code some functions for my own. 下一步:我想扩展MongoDatabase类型来为我自己编写一些函数。 Folling error comes up: java.lang.ClassCastException: com.mongodb.MongoDatabaseImpl cannot be cast to model.ownMongoDatabase
出现跟踪错误: java.lang.ClassCastException:com.mongodb.MongoDatabaseImpl无法转换为model.ownMongoDatabase
I don't know why, but the old Java-driver has an DB-Class. 我不知道为什么,但是旧的Java驱动程序有一个DB-Class。 Some changes are made to the Java-Driver (Version 3), and the function for the DB-Class are now marked as deprecated.
对Java驱动程序(版本3)进行了一些更改,现在将DB-Class的功能标记为已弃用。 Now you apperently have to use MongoDatabase.
现在,您必须使用MongoDatabase。 But MongoDatabase is an Interface, so no chance to extend it for my own Class?!
但是MongoDatabase是一个接口,因此没有机会为我自己的课程扩展它吗?
Code Snippet: 代码段:
public class MongodbInstance extends com.mongodb.MongoClient {
private ArrayList<ownMongoDatabase > _mongodbDatabases = new ArrayList<ownMongoDatabase >();
private void buildMongodbDatabases(){
MongoCursor<String> iterator = this.listDatabaseNames().iterator(); // iterate through Databases
while(iterator.hasNext()){
this._mongodbDatabases.add((ownMongoDatabase) this.getDatabase(iterator.next())); // add Database-Object to Array
}
}
}
class ownMongoDatabase implements MongoDatabase {
}
Is there an tought mistake/coding failure? 是否存在韧体错误/编码失败? Thanks for your help.
谢谢你的帮助。
this._mongodbDatabases.add((ownMongoDatabase) this.getDatabase(iterator.next())); this._mongodbDatabases.add((ownMongoDatabase)this.getDatabase(iterator.next()));
In this line you are trying to assign MongoDatabaseImpl to your ownMongoDatabase. 在此行中,您尝试将MongoDatabaseImpl分配给您自己的MongoDatabase。 This will for sure throw class cast exception.
这肯定会引发类强制转换异常。
Suppose you have class A which is parent class, Class B and Class C extends from Class A. You cannot assign class B to class C. 假设您有作为父类的类A,类B和类C从类A扩展。您不能将类B分配给类C。
Try has-A relationship instead of is-A relationship. 尝试使用has-A关系而不是is-A关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.