开发者

Having issues accessing a method

开发者 https://www.devze.com 2023-04-09 11:00 出处:网络
just a quick question in relation to accessing a method inside another class. I know it sounds simple but nowhere I look can answer it for me.. so here it goes.

just a quick question in relation to accessing a method inside another class. I know it sounds simple but nowhere I look can answer it for me.. so here it goes.

This is in one class.. (only leaving in the relevant bits because most of the other stuff is just formatting and passing around values.)

public class ContactManager extends JFrame{ 
   public void getData()throws FileNotFoundException{
   ...
   ...
       private class thehandler  implements ActionListener {
               public void actionPerformed(ActionEvent event) {
                    if (event.getSource() == viewButton && selectedInd开发者_运维技巧ex != -1){
                       ViewContact view = new ViewContact();
                       view.pack();
               view.setVisible(true);




public class ViewContact extends JFrame { 
            ... 
            ...
                private class viewhandler implements ActionListener {
                        public void actionPerformed(ActionEvent event){
                            if (event.getSource() == updateButton){                         
                                      getData(); //this method i want to use thats in the contact manager window is what im having issues with.
                 }

I know the syntax is wrong for accessing a method in another class I just wanted to show what i am trying to do. I cannot create another instance of the contact manager class because I am already using it.. :S at least i think that's the issue. I hope I have left in enough relevant information and thank you in advance for assistance with this.


The short answer to your question is that you can pass an appropriate instance of ContactManager to the ViewManager and call getData() as required:

class ViewManager extends JFrame {
    private ContactManager cm;

    public ViewManager(ContactManager cm) {
        this.cm = cm;
    }

    private class Handler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            cm.getData();
        }
    }
}

The longer answer is that it might be better to separate your business logic from your view logic and put the getData method into a business-layer class:

class DataSource {
    public Data getData();
}

class ContactManager extends JFrame {
    private DataSource dataSource;
    public ContactManager(DataSource dataSource) {
        ...
    }
}

class ViewContact extends JFrame {
    private DataSource dataSource;
    public ViewContact(DataSource dataSource) {
        ...
    }
}

This decouples the classes that are responsible for fetching/managing data from the classes that are responsible for displaying it. It means you can change the view without affecting the way data is stored, or you can change the way data is stored without affecting the view.


Why not create a constructor for ViewContact() that accepts a reference to your ContactManager instance? For example:

  ViewContact myContact = ViewContact (this);


You can call data()!

You can create another class-instance of ContactManager too.

You can also create instances of ViewContact using:

ContactManager m = new ContactManager();
JFrame contact = m.new ViewContact();

You even can instance thehandler using this:

    ContactManager manager = new ContactManager();
    Object b = null;
    for (Class<?> clazz : ContactManager.class.getDeclaredClasses()) {
        if (clazz.getSimpleName().equals("thehandler")){

            for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
                constructor.setAccessible(true);
                b=constructor.newInstance(manager);
                break;
            }
            break;
        }
    }
0

精彩评论

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

关注公众号