如何从java中的jar文件中实现多个接口? [英] How to implement multi interfaces from a jar file in java?
问题描述
我已经创建了一个类并导入我的接口,但是如何做其余的,我该怎么办?
这里我有什么,但它不工作平均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;
解决方案(){
ProcessingQueueFactoryClass runFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = runFactory.createQueue();
}
/ **您的ProcessingQueueFactory必须包含两种方法:一种用于创建新的
*队列,一种用于清理它们。在`createQueue`中,只需创建一个
* new的ProcessingQueue,在返回队列
*之前执行必要的初始化。在我们的
*自动化测试中,`createQueue`将被多次调用。在`stopQueue`中,执行您的`createQueue`方法创建的队列
*所需的任何清理。 (例如,停止线程,如果需要
*您的解决方案)。对于使用
*`createQueue'创建的每个队列,`stopQueue'将被调用一次。
* /
class ProcessingQueueFactoryClass实现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 implements 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(){
队列< String> newQueue = new LinkedList< String>();
return(ProcessingQueue)newQueue;
}
}
/ **
*您的ProcessingQueue实现将从多个并发生产者收到未处理的文本数据
*报价方法。您的队列
*必须通过其poll方法提供数据的转换版本。
*转换如下所述。作为一个队列,从
*`poll`收到的数据必须是对
*`offer`的调用的FIFO(先进先出)即第一项也应该是第一项。如果
*数据可用,则poll必须将其从队列中删除并返回。如果没有
*数据可用,那么`poll`必须返回null。
* /
class ProcessingQueueClass实现ProcessingQueue {
ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();
/ ** linkify转换应该在
*的输入文本中找到以http(s)://为前缀的原始URL,并将其转换为HTML链接。例如,
* http://www.example.com变为< a href =http://www.example.com> www.example.com< / a>。在锚文本中不要包含方案
*(http(s)://)。任何已经在HTML链接
*内的网址都不应该被修改。您可以假设输入文本包含由空格(即空格,新行)或标点符号分隔的多个单词
*(逗号,
*句点等)
* /
public String linkifyTransformation(String s){
// System.out.println(s);
String [] splitArray = s.split();
String convertedString =; (int i = 0; i< splitArray.length; ++ i){
if(splitArray [i] .startsWith(https://)){
convertedString + =< a href = \+ splitArray [i] +\>
+ splitArray [i] .substring(8)+< / a>;
} else if(splitArray [i] .startsWith(http://)){
convertedString + =< a href = \+ splitArray [i] +\ > 中
+ splitArray [i] .substring(7)+< / a>;
} else {
convertedString + = splitArray [i] +;
}
}
//System.out.println(transformedString);
return convertedString;
}
public boolean offer(String s){
//返回convertedString
linkifyTransformation;
//我们需要将convertedString添加到我们的队列中,我们应该在哪里创建?
//如何返回true或false?
返回false;
}
public String poll(){
return;
}
}
public static void main(String [] args){
// String s =快速http:// www .brown.com / fox
//跳转https://over.com懒狗foo www.bar.com
//是< a href = \http:// myfavorite。 com \>我最喜欢的< / a>
// +这些不是你要找的机器人。
//解决方案sol = new Solution();
// ProcessingQueueClass pqs = sol.new ProcessingQueueClass();
//pqs.linkifyTransformation(s);
}
}
以下是一些可能有帮助的细节。
Revmetrix Linkify队列编码通过转换来解决字符串数据的输入和输出问题。该队列必须
正确处理并发操作,性能合理。从JAR实现ProcessingQueue和ProcessingQueueFactory。
您的ProcessingQueue实现将通过其报价
方法从多个并发生产者处收到未处理的文本数据。您的队列
必须通过其 poll
方法提供数据的转换版本。作为队列,从 poll
收到的数据必须是对于提供的调用的FIFO(先进先出)
;
一般来说,当您实现
a interface
在java中,您需要实现它的方法并定义该接口中定义的方法的主体。
如果您不想实现所有该界面的方法体,您可以将类更改为抽象
类,并将方法标记为 abstract
。
我不知道你是多么熟悉 interfaces
和 classes
在java中,但我认为阅读一些教程,如 http://tutorials.jenkov.com/java/interfaces.html 将会有所帮助。
[修改问题后的编辑]
ClassCastException
正在发生: / p>
return(ProcessingQueue)newQueue;
因为您非法投递了一个 Queue< String>
到类型为 ProcessingQueue
的接口。这个演员不能发生,因为 Queue< String>
或者没有一个父母没有实现 ProcessingQueue
p>
但是由于你的分配规范,你的实现是不对的。
你应该实现一个类来实现 ProcessingQueue
,另一个实现 ProcessingQueueFactory
(现在你做了这一部分)。
您不应在 ProcessingQueueClass 中创建
ProcessingQueueFactoryClass
和 ProcessingQueue
code> class,所以删除这两行:
ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();
此外,您应该保留链接的队列
在
。因此,将以下行从 ProcessingQueue
类中的字符串 public ProcessingQueue createQueue()
方法移动到 ProcessingQueue
class:
队列< String> newQueue = new LinkedList< String>();
至
class ProcessingQueueClass实现ProcessingQueue {
Queue< String> newQueue = new LinkedList< String>();
...
}
在您的 public boolean offer(String s)
你应该得到 linkifyTransformation(s);
的结果,并添加到 newQueue
,而在 public String poll()
方法中,您可以删除然后返回第一个项目的 newQueue
来维护 FIFO
范例。
现在关于 ProcessingQueueFactoryClass
:
我这个类你有两个简单的方法。 createQueue()
方法的工作非常简单:只需创建一个 ProcessingQueueClass
类的实例并返回。 p>
另一种方法 stopQueue(ProcessingQueue p)
:此方法获取一个 ProcessingQueue
作为参数。因为你的类 ProcessingQueueClass
实现了 ProcessingQueue
,输入参数的运行时类型将是类型 ProcessingQueueClass
。并且因为您的 ProcessingQueueClass
类中有一个 Queue< String>
成员变量,因此此方法的目的是清除队列< String>
希望这将是有帮助的,
好运气。
I have created a class and import my interfaces but how to do the rest, what should I do?
Here I have anything but it doesn't work I mean ClassCastExeption doesn't work
code example :
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);
}
}
Here is some details which might help.
Revmetrix Linkify Queue Coding Problem the input and output of string data with a transformation. This queue must
correctly handle concurrent operations with reasonable performance. Implement ProcessingQueue and ProcessingQueueFactory from the JAR.
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. As a queue, the data received from poll
must be FIFO (first-in, first-out) with respect to calls to offer
;
In general, when you implement
an interface
in java you need to implement it's methods and define the body for the methods defined in that interface.
If you don't want to implement all of that interface's methods bodies, you would change your class into an abstract
class and mark that methods as abstract
too.
I don't know how familiar you are with interfaces
and classes
in java, but I think reading some tutorials like http://tutorials.jenkov.com/java/interfaces.html would be helpful.
[EDIT after Question Changed]
The ClassCastException
is happening here:
return (ProcessingQueue) newQueue;
because you illegally cast an instance of Queue<String>
to an interface of type ProcessingQueue
. This cast can not happen because Queue<String>
or none of it's parents didn't implement ProcessingQueue
interface.
But due to your assignment specification, your implementation is not right.
You should implement one class to implement ProcessingQueue
and another to implement ProcessingQueueFactory
(Til now you did this part).
You should not create instances of ProcessingQueueFactoryClass
and ProcessingQueue
in your ProcessingQueueClass
class, so remove these two line:
ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();
Also you should keep the Queue
of linkified Strings
in your ProcessingQueue
class. So move the following line from public ProcessingQueue createQueue()
method to ProcessingQueue
class:
Queue<String> newQueue = new LinkedList<String>();
to
class ProcessingQueueClass implements ProcessingQueue {
Queue<String> newQueue = new LinkedList<String>();
...
}
In your public boolean offer(String s)
you should get the result of linkifyTransformation(s);
and add it to the newQueue
and in the public String poll()
method you remove and then return the first item of newQueue
to maintain the FIFO
paradigm.
Now about the ProcessingQueueFactoryClass
:
I this class you have two simple methods. The work of createQueue()
method is very simple: Just create and instance of ProcessingQueueClass
class and return it.
And the other method stopQueue(ProcessingQueue p)
: This method gets an instance of ProcessingQueue
as an argument. Because your class ProcessingQueueClass
implemented ProcessingQueue
, the runtime type of input argument would be of type ProcessingQueueClass
. And because you have a Queue<String>
member variable in your ProcessingQueueClass
class, the purpose of this method is to clear the Queue<String> newQueue
items and release it.
Hope this would be helpful,
Good Luck.
这篇关于如何从java中的jar文件中实现多个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!