Java类扩展和继承

时间:2015-08-24 09:24:34

标签: java class inheritance properties constructor



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at airline.booking.system.BookingFrame.savebookingButtonActionPerformed(BookingFrame.java:357)
	at airline.booking.system.BookingFrame.access$200(BookingFrame.java:21)
	at airline.booking.system.BookingFrame$3.actionPerformed(BookingFrame.java:102)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6525)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6290)
	at java.awt.Container.processEvent(Container.java:2234)
	at java.awt.Component.dispatchEventImpl(Component.java:4881)
	at java.awt.Container.dispatchEventImpl(Container.java:2292)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
	at java.awt.Container.dispatchEventImpl(Container.java:2278)
	at java.awt.Window.dispatchEventImpl(Window.java:2750)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:702)
	at java.awt.EventQueue$3.run(EventQueue.java:696)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:724)
	at java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)




所以...过去3天一直在讨论这个问题。它有点难以解释我需要什么..但会尽力点。程序"使用"工作正常。但是我的导师告诉我改变我的预订课程...我的预订课程是扩展我的客户课程以获得其他属性,如姓名,姓氏,年龄等......但是(有道理)我被告知要制作我的客户是预订舱的财产而不是延伸。由于我不再扩展客户类,因此在尝试获取名称,姓氏等时,我的代码中出现错误,因为我不再从Customer类扩展。如何将客户作为财产包括在内,并仍然从客户类中获取客户属性。感谢



   private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) {                                             
        Customer customerfile = null;
        try {

            final JFileChooser chooser = new JFileChooser("Customers/");
            int chooserOption = chooser.showOpenDialog(null);
            if (chooserOption == JFileChooser.APPROVE_OPTION) {

                File file = chooser.getSelectedFile();
                ObjectInputStream in = new ObjectInputStream(
                        new FileInputStream(file)
                );

                customerfile = (Customer) in.readObject();

                custnameTF.setText(customerfile.getPersonName());
                custsurnameTF.setText(customerfile.getPersonSurname());
                custidTF.setText(customerfile.getPersonID());
                consnameTF.setText(customerfile.getConsultantname());
                conssurnameTF.setText(customerfile.getConsultantsurname());
                considTF.setText(customerfile.getConsulid());

                in.close();

            } else {
                throw new CancelException("Canceled Operation");
            }

        } catch (IOException ex) {
            System.out.println("Error Loading File" + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            System.out.println("Error Loading Class");
        } catch (CancelException ex) {
            JOptionPane.showMessageDialog(this, "Canceled Loading Customer",
                    "Canceled", JOptionPane.INFORMATION_MESSAGE);
        }
    }                                            

    private void savebookingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        Booking customerbooking = new Booking();
        Customer cust = customerbooking.getCustomer();

        try {
            if (custnameTF.getText().equals("")) {
                throw new EmptyField("Please Insert Customer");
            } else {
                FileOutputStream fos = new FileOutputStream("Bookings/" + custidTF.getText() + ".txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);

                cust.setPersonName((custnameTF.getText()));
                cust.setPersonSurname((custsurnameTF.getText()));
                cust.setPersonID((custidTF.getText()));
                cust.setConsultantname(consnameTF.getText());
                cust.setConsultantsurname((conssurnameTF.getText()));
                cust.setConsulid(considTF.getText());
                customerbooking.setFlightlocation(locationCB.getSelectedItem().toString());
                customerbooking.setFlighttime(timeCB.getSelectedItem().toString());
                customerbooking.setFlightfee(feeCB.getSelectedItem().toString());
                customerbooking.setCar(carRB.isSelected());
                customerbooking.setInsurance(insuranceRB.isSelected());

                oos.writeObject(customerbooking);
                oos.close();
                fos.close();

                custnameTF.setText("");
                custsurnameTF.setText("");
                custidTF.setText("");
                considTF.setText("");
                consnameTF.setText("");
                conssurnameTF.setText("");
                locationCB.setSelectedItem("");
                timeCB.setSelectedItem("");
                feeCB.setSelectedItem("");

                JOptionPane.showMessageDialog(this, "Booking was Saved Successfully!",
                        "Success", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Booking could not be Saved!",
                    "Error!", JOptionPane.INFORMATION_MESSAGE);
        } catch (EmptyField ex) {
            JOptionPane.showMessageDialog(this, "Please Insert Customer",
                    "Error", JOptionPane.INFORMATION_MESSAGE);
        }

        dispose();

    }                                                 

    private void custnameTFActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void custidTFActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void loadbookingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        Booking bookingfile = null;
        Customer custfile = null;
        try {

            final JFileChooser chooser = new JFileChooser("Bookings/");
            int chooserOption = chooser.showOpenDialog(null);
            if (chooserOption == JFileChooser.APPROVE_OPTION) {

                File file = chooser.getSelectedFile();
                ObjectInputStream in = new ObjectInputStream(
                        new FileInputStream(file)
                );

                bookingfile = (Booking) in.readObject();

                custnameTF.setText(custfile.getPersonName());
                custsurnameTF.setText(custfile.getPersonSurname());
                custidTF.setText(custfile.getPersonID());
                consnameTF.setText(custfile.getConsultantname());
                conssurnameTF.setText(custfile.getConsultantsurname());
                considTF.setText(custfile.getConsulid());
                locationCB.setSelectedItem(bookingfile.getFlightlocation());
                timeCB.setSelectedItem(bookingfile.getFlighttime());
                feeCB.setSelectedItem(bookingfile.getFlightfee());
                carRB.setSelected(bookingfile.getCar());
                insuranceRB.setSelected(bookingfile.getInsurance());

                in.close();

            } else {
                throw new CancelException("Canceled Operation");
            }

        } catch (IOException ex) {
            System.out.println("Error Loading File" + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            System.out.println("Error Loading Class");
        } catch (CancelException ex) {
            JOptionPane.showMessageDialog(this, "Canceled Loading Booking",
                    "Canceled", JOptionPane.INFORMATION_MESSAGE);
        }
    }                                                 

    private void createCustbtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
        CustomerFrame nc = new CustomerFrame();
        nc.setVisible(true);
    }                                             




2 个答案:

答案 0 :(得分:0)

我不确定我是否清楚地了解你。但尝试这样的事情:

Booking customerbooking = new Booking();
Customer cust = customerbooking.getCustomer();
cust.setPersonName((custnameTF.getText()));
cust.setPersonSurname((custsurnameTF.getText()));

您无法从Customer调用Booking的方法。首先,您需要引用Customer对象。

你不能这样做:

customerbooking.setPersonName((custnameTF.getText()));
customerbooking.setPersonSurname((custsurnameTF.getText()));

答案 1 :(得分:0)

你的导师想要的可能是这样的。如果没有,请更具体地说明问题。

class Customer {
    private int someVal = 42;
   // lots of instance variables and methods
   public int getSomeVal() {
      return someVal;
   }
}

class Booking {
  Customer c;
  int fortytwo;
  // more variables
  public Booking() {}
  public Booking(Customer c) {
    this.c = c;
    fortytwo = c.getSomeVal();
    // more stuff
  }
  // more code
}

主:

Customer c = new Customer();
Booking b = new Booking(c);

通过客户参考,您现在可以访问预订类中的客户方法,或者您拥有客户对象的其他方法。