【问题标题】:How to implement multi interfaces from a jar file in java? [duplicate]如何从java中的jar文件实现多接口? [复制]
【发布时间】:2016-02-06 10:15:01
【问题描述】:

我已经创建了一个类并导入了我的接口,但剩下的怎么办,我该怎么办?

我有任何东西,但它不起作用我的意思是 ClassCastExeption 不起作用

代码示例:

import java.util.LinkedList;
import java.util.Queue;

import com.revmetrix.code_test.linkify_queue.ProcessingQueue;
import com.revmetrix.code_test.linkify_queue.ProcessingQueueFactory;

public class Solution {

    ProcessingQueue newQueue;
    ProcessingQueueFactory runFactory;

    Solution() {
        ProcessingQueueFactoryClass runFactory = new ProcessingQueueFactoryClass();

        ProcessingQueue newQueue = runFactory.createQueue();

    }

    /**Your ProcessingQueueFactory must contain two methods: one for creating new
     * queues and one for cleaning up after them. In `createQueue`, just create a
     * new ProcessingQueue, performing any necessary initialization of the queue
     * before it is returned. `createQueue` will be called multiple times during our
     * automated tests. In `stopQueue`, perform any cleanup required for a queue
     * created by your `createQueue` method. (E.g., stop threads, if necessary for
     * your solution.) `stopQueue` will be called once for each queue created with
     * `createQueue`.
     */
    class ProcessingQueueFactoryClass implements ProcessingQueueFactory {

        public void stopQueue(ProcessingQueue p) {

        }


        // TODO encok burda sikinti var, queue yaratacam ama Proccesing Queue donderiyor bu
        // asil eleman ekleyecegim queue yi ProcessingQueue nin icinde mi yaratcam?
        // ProcessingQueue bi interface bu arada, bunu implement eden class ProcessingQueueClass yazdim 
        // onun icinde queue olsun dedim yine gormuyor zalim queue olarak 
        //bi loop var ProcessingQueue ile ProcessingQueueFactory arasinda, anlamadim!
        public ProcessingQueue createQueue() {



            Queue<String> newQueue = new LinkedList<String>();

            return (ProcessingQueue) newQueue;
        }

    }

    /**
     * Your ProcessingQueue implementation will receive unprocessed textual data
     * from multiple concurrent producers through its `offer` method. Your queue
     * must provide a transformed version of the data via its `poll` method. The
     * transformation is described below. As a queue, the data received from
     * `poll` must be FIFO (first-in, first-out) with respect to calls to
     * `offer`; i.e., the first items in should also be the first items out. If
     * data is available, `poll` must remove it from queue and return it. If no
     * data is available, then `poll` must return null.
     */
    class ProcessingQueueClass implements ProcessingQueue {

        ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
        ProcessingQueue newQueue = openFactory.createQueue();

        /**The linkify transformation should find raw URLs prefixed with "http(s)://" in
         * the input text and convert them to HTML links. For example,
         * http://www.example.com becomes <a href="http://www.example.com">www.example.com</a>. Do not include the scheme
         * (http(s)://) in the anchor text. Any URLs that are already within HTML links
         * should not be modified. You can assume the input text contains multiple words
         * separated by white space (i.e. spaces, new lines) or punctuations (commas,
         * periods, etc.)
         */
        public String linkifyTransformation(String s){
            // System.out.println(s);
            String[] splitArray = s.split(" ");
            String transformedString = "";

            for (int i = 0; i < splitArray.length; ++i) {
                if (splitArray[i].startsWith("https://")) {
                    transformedString += "<a href=\"" + splitArray[i] + "\">"
                            + splitArray[i].substring(8) + "</a> ";
                } else if (splitArray[i].startsWith("http://")) {
                    transformedString += "<a href=\"" + splitArray[i] + "\">"
                            + splitArray[i].substring(7) + "</a> ";
                } else {
                    transformedString += splitArray[i] + " ";
                }
            }

            //System.out.println(transformedString);            
            return transformedString;
        }

        public boolean offer(String s) {

            //returns transformedString 
            linkifyTransformation(s);
            // we need to add the transformedString to our Queue, Where should we create?;

            //how to return true or false?
            return false;
        }

        public String poll() {

            return "";
        }

    }

    public static void main(String[] args) {
//      String s = "The quick http://www.brown.com/fox 
//                 jumps https://over.com the lazy dog foo www.bar.com 
//                 is <a href=\"http://myfavorite.com\">my favorite</a> "
//                 + "These aren't the droids you're looking for.";

        //Solution sol = new Solution();
        //ProcessingQueueClass pqs = sol.new ProcessingQueueClass();
        //pqs.linkifyTransformation(s);


}

}

这里有一些可能会有所帮助的细节。

Revmetrix Linkify 队列编码问题使用转换的字符串数据的输入和输出。这个队列必须 以合理的性能正确处理并发操作。从 JAR 实现 ProcessingQueue 和 ProcessingQueueFactory。 您的 ProcessingQueue 实现将通过其offer 方法从多个并发生产者接收未处理的文本数据。你的队列 必须通过其poll 方法提供数据的转换版本。作为队列,从poll收到的数据 对于对 offer 的调用,必须是 FIFO(先进先出);

【问题讨论】:

  • 我需要帮助,一点帮助,而不是警告,不,它不是重复的,因为我专注于两个接口,我从一个 jar 文件中导入它们,但是如何填充后者?
  • 您在上述情况下遇到的具体问题是什么?

标签: java eclipse jar interface


【解决方案1】:

一般来说,当你在 java 中 implementinterface 时,你需要实现它的方法并为该接口中定义的方法定义主体。

如果您不想实现该接口的所有方法主体,则可以将您的类更改为 abstract 类并将该方法也标记为 abstract

我不知道你对java中的interfacesclasses有多熟悉,但我认为阅读一些类似http://tutorials.jenkov.com/java/interfaces.html的教程会有所帮助。

[问题更改后编辑]

ClassCastException 正在这里发生:

return (ProcessingQueue) newQueue;

因为您非法将Queue&lt;String&gt; 的实例强制转换为ProcessingQueue 类型的接口。这个演员不能发生,因为Queue&lt;String&gt; 或者它的父母都没有实现ProcessingQueue 接口。

但是由于您的分配规范,您的实现不正确。

你应该实现一个类来实现ProcessingQueue,另一个实现ProcessingQueueFactory(直到现在你已经完成了这部分)。

您不应在 ProcessingQueueClass 类中创建 ProcessingQueueFactoryClassProcessingQueue 的实例,因此请删除这两行:

ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();

您还应该在您的ProcessingQueue 类中保留链接的StringsQueue。所以将下面这行从public ProcessingQueue createQueue() 方法移动到ProcessingQueue 类:

Queue<String> newQueue = new LinkedList<String>();

class ProcessingQueueClass implements ProcessingQueue {
    Queue<String> newQueue = new LinkedList<String>();
    ...
}

在您的public boolean offer(String s) 中,您应该得到linkifyTransformation(s); 的结果并将其添加到newQueue 并在public String poll() 方法中您删除,然后返回 第一项newQueue 维护FIFO 范式。

现在关于ProcessingQueueFactoryClass: 我这个类你有两个简单的方法。 createQueue() 方法的工作非常简单:只需创建 ProcessingQueueClass 类的实例并返回即可。

另一个方法stopQueue(ProcessingQueue p):该方法获取ProcessingQueue 的实例作为参数。因为您的类ProcessingQueueClass 实现了ProcessingQueue,所以输入参数的运行时类型将是ProcessingQueueClass 类型。并且因为你的ProcessingQueueClass类中有一个Queue&lt;String&gt;成员变量,所以这个方法的目的是清除Queue&lt;String&gt; newQueue项并释放它。

希望这会有所帮助,

祝你好运。

【讨论】:

  • 非常感谢,我想在java中实现两个接口,所以我需要实现它的方法并为该接口中定义的方法定义主体。
  • 欢迎您,我也强烈建议您仔细阅读该教程。如果您觉得这篇文章对您有帮助,您可以通过勾选这篇文章左侧的绿色复选标记将其标记为答案。
  • 你能再检查一下吗,你的解决方案迫使我做很多事情,但仍然有错误。
  • @Ada:请提供一些关于究竟是什么问题的信息?有例外吗?提供您正在运行的代码以及异常堆栈跟踪。现在你的主要方法都被注释了,我们没有你的接口库来运行你的代码。所以请提供更准确的信息。
  • 非常感谢,可以联系吗?
猜你喜欢
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 2012-04-14
相关资源
最近更新 更多