【问题标题】:Java getter() and setter()Java getter() 和 setter()
【发布时间】:2015-08-25 11:27:28
【问题描述】:

有人能告诉我为什么我不能正确使用 Set 吗?我设法使用 Get 在文本框中加载姓名、姓氏等,但 Set 不允许我从文本框中读取并保存它们。谢谢

 custnameTF.setText(bookingfile.getCustomer().getPersonName());
 custsurnameTF.setText(bookingfile.getCustomer().getPersonSurname());
customerbooking.setCustomer(.setPersonName(custnameTF.getText));
public class Booking implements Serializable{

    private String flighttime;
    private String flightlocation;
    private String flightfee;
    private boolean car;
    private boolean insurance;
    private Customer customer;

我正在尝试将 2 个类连接在一起而无需扩展。我想通过 Booking 类加载和保存 Customer 类。谢谢

【问题讨论】:

  • (.setPersonName(custnameTF.getText));它不会编译。请更正您的问题并提供更多详细信息,因为几乎不可能猜到。

标签: java class get set


【解决方案1】:

首先,这不是要设置的正确语法。

由于setCustomer 只接受Customer 对象,您需要创建一个Customer 对象,然后将其设置为bookingCustomer

Customer customer= new Customer();
customer.setPersonName(custnameTF.getText); // look at the correct syntax.
//set remaining properties to customer objects from text fields
// ..
//then
customerbooking.setCustomer(customer);

【讨论】:

    【解决方案2】:

    Setter 通常不返回任何内容,这就是为什么不能以另一个 set 方法的返回值(返回 void)作为参数调用另一个 set 方法的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 2013-07-09
      相关资源
      最近更新 更多