I'm trying to repaint() from another class. I know one way to do this:
//FromClass.java:
SomeClass whatever = new SomeClass(this);
//SomeClass.java:
FromClass f;
public SomeClass(FromClass from){ //constructor
f = from;
}
//after a long part of code
f.repaint();
Is there any other way to do this, without any parameters in class constructor? And sorry for my technical English, still learning.
There are numerous ways to share information between classes. I believe that the method you demonstrated is the most effective. Here are some other options:
Make SomeClass
a subclass of FromClass
so that it inherits all of the methods defined in FromClass
. Then create an instance of FromClass
and call methods on the variable. Your class declaration would look like this:
class SomeClass extends FromClass { ... super.repaint(); ....
Make FromClass
an interface and implement it:
public interface FromClass { void repaint(); } class SomeClass implements FromClass { repaint();
Just some other alternatives. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.