开发者

How does an object pass itself as a parameter?

I have a JFrame that is the main class of the program. I want to pass him itself to another two classes, one that will update some label with statistics and another that will validate a lot of fields.

I done the getters to these fields, but how I do to the JFrame pass itself to these classes to they can do their work?

EDIT: My error, my fault. The first thing I done is the this method. Yes, this solves my problem, but it did not realize that I was making a mistake.

I was doing this: new Statistics().execute(this);

where the right thing is this: new Statistics(this).execute();

Only with all the answers saying the same thing I realized tha开发者_如何转开发t I was doing a stupid thing. Thanks to all.


Just pass a reference to this

public class Other {

   public void doSomething(JFrame jFrame) {
      ...
   }
} 

public class MyFrame extends JFrame {

   Other other = new Other();

   public void method() {
      other.doSomething(this);
   }

}


The pseudo-variable this always points to the current object in an instance method. Just pass this as the argument where you want the reference to go.


You can use this to pass itself within a method.

foo(this);


did you try using this ?

For example:

other.doSomething(this)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜