谁能帮我有个问题,请帮忙!!

03-01-13 andyj2ee

banq 你好,我有个问题,请帮忙!! 发表时间: 2003年01月02日 14:32:41 回复

DatabaseObjectIterator.java 中有如下构造器我不明白。

extraObject 是Object 的类型。为什么能:ForumFactory factory = (ForumFactory)extraObject;

在DbForum 中传过来的只是this.它只实现了Forum, //public class DbForum implements Forum, Cacheable //

且我只看到有一个private DbForumFactory factory; 变量。

return new DatabaseObjectIterator(JiveGlobals.THREAD, popularThreads,this);

public DatabaseObjectIterator(int type, long [] elements,

final Object extraObject)

{

this.elements = elements;

// Load the appropriate proxy factory depending on the type of object

// that we're iterating through.

switch (type) {

// FORUM

case JiveGlobals.FORUM:

// Create an objectFactory to load forums.

this.objectFactory = new DatabaseObjectFactory() {

ForumFactory factory = (ForumFactory)extraObject;

public Object loadObject(long id) {

try {

Forum forum = factory.getForum(id);

return forum;

。。。。。。。。。。。。。。。。。。

andyj2ee
2003-01-13 01:08

我作了一个测试如下:报错啊。!!

谁能帮我看看。

class Forum{};

class ForumFactory {

public Forum getForum(long id) {

return new Forum();

}

}

interface DatabaseObjectFactory {

public Object loadObject(long id);

}

class DatabaseObjectIterator {

private DatabaseObjectFactory objectFactory;

public DatabaseObjectIterator(final Object extraObject)

{

this.objectFactory = new DatabaseObjectFactory() {

ForumFactory factory = (ForumFactory)extraObject;

public Object loadObject(long id) {

Forum forum = factory.getForum(id);

return forum;

}

};

}

}

class DBIteratorTest

{

public static void main(String[] args)

{

System.out.println("Hello World!");

DBIteratorTest db = new DBIteratorTest();

new DatabaseObjectIterator(db);

}

}

//

/*

---------- java ----------

Hello World!

java.lang.ClassCastException

at DatabaseObjectIterator$1.<init>(DBIteratorTest.java:24)

at DatabaseObjectIterator.<init>(DBIteratorTest.java:23)

at DBIteratorTest.main(DBIteratorTest.java:42)

Exception in thread "main" Normal Termination

Output completed (0 sec consumed).

*/

andyj2ee
2003-01-14 19:16

不用回答了。我明白了。

hxz
2003-01-20 11:06

到底是什么啊?

我今天看了好久啊!

hxz
2003-01-20 11:45

严重gz