• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to cover or invoke anonymous inner class??

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my class





How to print the two sentence ??
Please help me
P.S. codes are for learning only
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What print functions? You do not have any functions in that code.
To call the methods of Listeners, you would need an event object which they are listening for. Why are you using listener interfaces of your own which the Java platform is not aware of?
For the Runnable, you would have to pass that to the constructor of a Thread and call the start method.
 
Qiang Qiang Xi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What print functions? You do not have any functions in that code.
To call the methods of Listeners, you would need an event object which they are listening for. Why are you using listener interfaces of your own which the Java platform is not aware of?
For the Runnable, you would have to pass that to the constructor of a Thread and call the start method.




Sorry, I mean the two sentences.

that's an example , the real codes are more or less the same. I just want to know how to print out the sentence enclosed in it.
Then, what do you mean by the start method?? Are you trying to say " test t=new test(); t.go() ; "?(but it doesn't work)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chuen abc wrote:
what do you mean by the start method?



Please see the api docs for the Thread class (that implements Runnable).

You could use the constructor:
public Thread(Runnable target)

It has a simple example on it, that leads up to the code:

PrimeRun p = new PrimeRun(143);
new Thread(p).start();

That way you would be able to print the statements in the run method.
 
Qiang Qiang Xi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please see the api docs for the Thread class (that implements Runnable).

You could use the constructor:
public Thread(Runnable target)

It has a simple example on it, that leads up to the code:

PrimeRun p = new PrimeRun(143);
new Thread(p).start();

That way you would be able to print the statements in the run method.



I am so sorry that I cannot get it.
Dream p = new Dream();
new Thread(p).start();

I try the above codes ,but still cannot print the result.
 
Rommel Sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chuen abc wrote:
I am so sorry that I cannot get it.
Dream p = new Dream();
new Thread(p).start();

I try the above codes ,but still cannot print the result.




I was referring you to read up the Java API docs for clearing the concept in discussion as it is a very good starting point and what I noted was an example from there.

For running the code you have pasted, made some minor changes to fix the code:



It gives me the output:


That's Great
Thread-0

 
Qiang Qiang Xi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method is good. But I am not allowed to change the class test, what can I do to invoke it??(Or there should be something in the test, so that I can invoke them )
By the way,do you know how to invoke the gogo class's println.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic