简体   繁体   English

Java - 从另一个类更新对象

[英]Java - Update Object from another class

How can I update an object in class A from a method in class B without using the return? 如何在不使用返回的情况下从B类中的方法更新A类中的对象?

for example: 例如:

public class A {
//my main class

private javax.swing.JTextField txtField1;
//a text field (txtField1) is initialized in this class and drawn

}


public class B {

public void doSomething(){

    //does something and updates the txtField1 in class A

}

}

and once again, I do not wish to use return since my return is already returning another value from the same method. 再一次,我不希望使用return,因为我的返回已经从同一个方法返回另一个值。

There are many ways you could achieve this. 有很多方法可以实现这一目标。 The simplest would be to pass the object into the method in class B: 最简单的方法是将对象传递给B类中的方法:

public void doSomething(JTextField fieldToUpdate){
   //your logic here
   fieldToUpdate.setText("something");
}

Then you can just update fieldToUpdate directly. 然后你可以直接更新fieldToUpdate This is not a great design pattern since it directly exposes control of a variable owned by 1 class to another. 这不是一个很好的设计模式,因为它直接暴露了1类拥有的变量对另一个类的控制。

Another alternative is to pass the instance of Class A into the method and call public methods on it: 另一种方法是将类A的实例传递给方法并在其上调用公共方法:

public void doSomething(A aInstance){
    //your logic here
    aInstance.setText("something");
}

then in class A you'd need to define 然后在A级你需要定义

public void setText(String text){
   txtField1.setText(text);
}

This is a little better since class B doesn't have direct access to the internals of Class A. 这是一个更好的,因为B类没有直接访问A类的内部。

An even more encapsulated response (though probably overkill for a case this simple) is to define an Interface and pass an instance of a class that implements the interface to the method in class B: 一个更加封装的响应(尽管对于这种简单的情况可能有点过分)是定义一个接口并将实现接口的类的实例传递给B类中的方法:

public void doSomething(TextDisplayer txt){
  //your logic here
   txt.setText("something");
}

then in class a: 然后在课堂上:

public class A implements TextDisplayer{

 public void setText(String txt){
  txtField1.setText(txt);
}

}

then the interface: 那么界面:

public interface TextDisplayer{
 public void setText(String txt);
}

The advantage of this approach is that it keeps class B completely decoupled from the class A. All it cares about is that it is passed something that knows how to handle the setText method. 这种方法的优点是它可以使B类与A类完全分离。它所关心的是传递了一些知道如何处理setText方法的东西。 Again, in this case it is probably overkill, but it is the approach that keeps your classes as decoupled as possible. 同样,在这种情况下,它可能有点过分,但它是使您的类尽可能分离的方法。

You either need to call a method in class A , or make the text field static (bad idea). 您需要在类A调用方法,或者使文本字段为静态(不好主意)。

Depending on usage, class A could instantiate a B as a swing worker/etc. 根据使用情况, A类可以将B实例化为摇摆工人/等。 and give B the specific information it needs. 并给B所需的具体信息。 It could also be the other way around, B instantiates an `A. 它也可能是另一种方式, B实例化`A.

assume you have a public setter method in A to change the value of txtField1. 假设您在A中有一个公共setter方法来更改txtField1的值。 (because the property you want to change has keyword "private") (因为您要更改的属性具有关键字“private”)

say in A, you have 在A中说,你有

public void setTxtField1Value(String newValue){
this.txtField1.value=newValue; // using the right method in api. I am not familiar with gui..
}

then in B, the method would be: 然后在B中,方法是:

public class B {

public void doSomething(A a){

    //does something and updates the txtField1 in class A
a.setTxtField1Value("foobar");

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM