2

我有一个运行方法,它试图覆盖另一个运行方法。但它没有发生,因为在它传递给运行方法之前我得到了一个“类未找到异常”。

这是我的带有运行方法的课程

我怎么能超越 run 方法。

我必须调用才能执行的类。

    public abstract class MessageProcessor implements Runnable {
private Collection<KpiMessage> fetchedMessages;
private Connection dbConnection;
Statement st = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
private Collection<KpiMessage> outgoingQueue;


public KpiMsg804 MessageProcessor(Collection<KpiMessage> outgoingQueue, Connection
 dbConnection){
    this.outgoingQueue = outgoingQueue;
    this.dbConnection = dbConnection;
    return (KpiMsg804) fetchedMessages;
}

    public Collection<KamMessage> generate(Collection<KamMessage> outgoingQueue)
   {
    while(true){
        try {

            while (rs.next()) {
                KamMessage filedClass = convertRecordsetToPojo(rs);
                outgoingQueue.add(filedClass);

            }

            for (KamMessage pojoClass : outgoingQueue) {
                KamMsg804 updatedValue = createKamMsg804(pojoClass);
                System.out.print(" " + pojoClass.getSequence());
                System.out.print(" " + pojoClass.getTableName());
                System.out.print(" " + pojoClass.getAction());
                System.out.print(" " + updatedValue.getKeyInfo1());
                System.out.print(" " + updatedValue.getKeyInfo2());
                System.out.println(" " + pojoClass.getEntryTime());
            }
            return outgoingQueue;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}

4

1 回答 1

3

在您的代码中,您正在类中创建一个默认可见性字段,而不是覆盖该run()方法。只要坚持:

MessageProcessor processor = new MessageProcessor() {
  @Override
  final public void run() {
    MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
  }
};

并删除外部MessageProcessor声明。

我制作了您的代码的一个子集:

public class PollingSynchronizer {
  public static void main(String[] args) {
    MessageProcessor message = new MessageProcessor() {
      MessageProcessor message = new MessageProcessor() {
        public void run() {
          System.out.println("new run");
        }
      };
    };

    new Thread(message).start();
  }
}

class MessageProcessor implements Runnable {
  public void run() { System.out.println("old run"); }
}

哪个会打印old run,因为第一个MessageProcessor::run()它并没有真正被覆盖。

于 2013-01-18T12:15:42.003 回答