对象如何将自身作为参数传递?

时间:2021-09-06 16:59:56

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.

我有一个JFrame,它是该程序的主要类。我想把他自己传递给另外两个类,一个用统计数据更新一些标签,另一个用于验证很多字段。

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?

我对这些字段做了getters,但是我如何对JFrame传递给这些类,他们可以做他们的工作?

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();

我这样做:new Statistics()。execute(this);这是正确的事情:new Statistics(this).execute();

Only with all the answers saying the same thing I realized that I was doing a stupid thing. Thanks to all.

只有所有答案都说同样的事情我才意识到我做了一件蠢事。谢谢大家。

4 个解决方案

#1


14  

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);
   }

}

#2


6  

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.

伪变量this始终指向实例方法中的当前对象。只需将此作为您想要引用的参数传递。

#3


3  

You can use this to pass itself within a method.

您可以使用它在方法中传递自己。

foo(this);

#4


3  

did you try using this ?

你尝试过这个吗?

For example:

other.doSomething(this)

#1


14  

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);
   }

}

#2


6  

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.

伪变量this始终指向实例方法中的当前对象。只需将此作为您想要引用的参数传递。

#3


3  

You can use this to pass itself within a method.

您可以使用它在方法中传递自己。

foo(this);

#4


3  

did you try using this ?

你尝试过这个吗?

For example:

other.doSomething(this)