【问题标题】:Java passing values to another frameJava将值传递给另一个框架
【发布时间】:2016-08-07 01:21:10
【问题描述】:

我正在创建一个员工管理系统,我想知道在员工成功登录后如何将员工的详细信息从登录框架传递到其他框架。

我目前的方法是在成功登录时检索他们的数据并将其传递给构造函数。

if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
                    close();
                    String name = temp.getName();
                    String position = temp.getPosition();
                    String imageSrc = temp.getImageSRC();
                    String email = temp.getEmail();
                    Home page = new Home(staffId,name,position,imageSrc,email);
                    page.setVisible(true);
                    MainInterface menu = new MainInterface(staffId,name,position,imageSrc,email);
                    form b = new form(staffId,name,position,imageSrc,email);
                    Patients a = new Patients(staffId,name,position,imageSrc,email);
                    AdminMenu admin = new AdminMenu(staffId,name,position,imageSrc,email);
                    MainRpt r = new MainRpt(staffId,name,position,imageSrc,email);
                    viewSchedule s = new viewSchedule(staffId,name,position,imageSrc,email);
                }

它有效,但我想知道是否有其他方法可以做到这一点。

【问题讨论】:

    标签: java oop


    【解决方案1】:

    可以通过构造函数传递你的参数,但创建一个 Object Staff 来封装你需要从一个类传递到另一个类的所有字段更加灵活。

    public class Staff {
        private String name;
        private String position;
        private String imageSrc;
        private String email;
    
        public Staff(Object temp){ // Change object here to the "temp" type
           this.name = name;
           //...
        }
    }
    

    然后将接收类的构造函数更改为类似

    public Home(Staff staff){
    
    }
    

    这样你就可以创建Home

    Staff staff = new Staff(temp); 
    Home page = new Home(staff);
    

    【讨论】:

      【解决方案2】:

      当使用框架时,通常你有一个更大的类,一个控制器,它包含视图并将它们与模型连接(检查MVC)。

      长话短说,你应该(你没有不得不这样做,但在我看来这是最好的方法)做这样的事情:

      注意:这与您的代码无关,只是为了展示 MVC 的示例。

      class Model {
          int firstNumber;
          int secondNumber;
      
          public int add () {
              return firstNumber + secondNumber;
          }
      }
      
      class View extends JFrame {
      
          JTextArea first;
          JTextArea second;
      
          JLabel result;
      
          // do necessary stuff to show the JFrame, and add this TextAreas and the label
      }
      
      class Controller {
          Model model;
          View view;
      
          // Initialize both in the constructor
      
          public void add() { // This can be called when you press certain button, for example
              model.firstNumber = view.first;
              model.secondNumber = view.second; // THIS IS PSEUDO CODE, you have to convert it and stuff
      
              view.result = model.add();
          }
      }
      

      你应该在你的程序中做这样的事情,一切都会变得更容易。

      如果您不想这样做,那么最适合您的解决方案可能就是您正在做的事情或类似的事情,但这更容易事后修改,也更容易理解。

      【讨论】:

        【解决方案3】:

        这样怎么样?你不需要一次又一次地传递很多参数。我只是建议你,但我还没有测试过。

        if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
            close();
            StaffInfo staffInfo =  new StaffInfo();
            staffInfo.setName(temp.getName());
            staffInfo.setPosition(temp.getPosition());
            staffInfo.imageSrc(temp.getImageSRC());
            staffInfo.setEmail(temp.getEmail());
        
            Home page = new Home(staffInfo);
            page.setVisible(true);
            MainInterface menu = new MainInterface(staffInfo);
        
            form b = new form(staffInfo);
            Patients a = new Patients(staffInfo);
            AdminMenu admin = new AdminMenu(staffInfo);
            MainRpt r = new MainRpt(staffInfo);
            viewSchedule s = new viewSchedule(staffInfo);
        }         
        
        public class StaffInfo {
        
            private String name ;
            private String position;
            private String imageSrc;
            private String email;
        
            // Getters and setters
        }
        

        【讨论】:

          【解决方案4】:

          如果需要向方法(或构造函数)传递太多参数,最好将它们包装在一个新类中并将该类的实例传递给该方法。

          【讨论】:

            猜你喜欢
            • 2014-06-28
            • 2015-07-12
            • 2014-02-24
            • 1970-01-01
            • 2019-05-27
            • 2018-10-02
            • 2021-12-31
            • 1970-01-01
            相关资源
            最近更新 更多