开发者

Is it possible to implement an interface inside a constructor?

开发者 https://www.devze.com 2023-03-02 15:26 出处:网络
The question may sees to be senseless. But could anybody clarify me the bit of coding that i have attatched with this question. I\'m in a college project related to parsing. So i was refering HtmlClea

The question may sees to be senseless. But could anybody clarify me the bit of coding that i have attatched with this question. I'm in a college project related to parsing. So i was refering HtmlCleaner. I got stucked with this coding.

final CleanerProperties props = new CleanerProperties();
final HtmlCleaner htmlCleaner = new HtmlCleaner(props);
final SimpleHtmlSerializer htmlSerializer = 
    new SimpleHtmlSerializer(props);

// make 10 开发者_StackOverflowthreads using the same cleaner and the same serializer 
for (int i = 1; i <= 10; i++) {
    final String url = "http://search.eim.ebay.eu/Art/2-1/?en=100&ep=" + i;
    final String fileName = "c:/temp/ebay_art" + i + ".xml";
    new Thread(new Runnable() {
        public void run() {
            try {
                TagNode tagNode = htmlCleaner.clean(new URL(url));
                htmlSerializer.writeToFile(tagNode, fileName, "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

Can we implement an interface inside a constructor?(Thread class,Runnable interface).Could anybody help me to understand the concept behind it or suggest some articles to study that concept? Thanks in advance...


You're declaring an anonymous class that "inherits from" (or implements, in this case) a Runnable.

The Thread is just using an existing Thread constructor (one that accepts a Runnable). Anonymous classes are part of Java (and have been for a long time): http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#252986


In your example, you don't create an interface inside a constructor.

The snippet shows the implementation of an anonymous subclass of Runnable. anonymous because the type of this class simply has no name.

The new Runnable(... statement creates a reference to an instance of that anonymous class and that reference is passed to the constructor Thread(Runnable r).


Note - we can do the same in three steps, which is a little easier to understand:

// create an anonymous implementation of Runnable
Runnable r = new Runnable() {
     @Override
     public void run() {
       // the run implementation
     }
   };

// create a Thread
Thread t = new Thread(r);

// start the Thread -> will call the run method from the Runnable
t.start();


that's an anonymous class. refer : http://ssmela.googlepages.com/AnonymousClassesinJava.pdf


The command new can be used along with an interface only in a situation like this, where you immediately define the methods.


new Thread(new Runnable() {
    public void run() {
        try {
            TagNode tagNode = htmlCleaner.clean(new URL(url));
            htmlSerializer.writeToFile(tagNode, fileName, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

new Runnable() {...} is declaring an Anonymous Inner Classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消