• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to cover or invoke anonymous inner class??

 
Qiang Qiang Xi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • 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
  • 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)
 
Rommel Sharma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic