读取位于私有方法中的变量

时间:2010-11-02 16:39:09

标签: java variables println

我遇到一个小问题我正在尝试打印位于私有方法中的几个变量的内容。但我只是继续“找不到符号”

下面是我试图从(包括println)读取数据的代码,我也是java的新手。

private void createBooking()
{
     String title;
     String firstName;
     String lastName;
     String bookingNo;
     String roomType;

    System.out.print("Enter title: ");
    title = keyboard.next();
    System.out.print("Enter first name: ");
    firstName = keyboard.next();
    System.out.print("Enter last name: ");
    lastName = keyboard.next();
    System.out.print("Enter booking number: ");
    bookingNo = keyboard.next();
    System.out.print("Enter room type: ");
    roomType = keyboard.next();


    aBooking = new Booking (title, firstName, lastName, bookingNo, roomType);

}

public void printCustomerName()
{
    System.out.println (createBooking.title);
}

5 个答案:

答案 0 :(得分:4)

您可能希望将这些变量设为member variables,然后只需访问它而不使用. - 运算符。

class BookingClass {

    // You also seem to need the following:
    Booking aBooking;

    String title;
    String firstName;
    String lastName;
    String bookingNo;
    String roomType;

    private void createBooking() {

        System.out.print("Enter title: ");
        title = keyboard.next();
        System.out.print("Enter first name: ");
        firstName = keyboard.next();
        System.out.print("Enter last name: ");
        lastName = keyboard.next();
        System.out.print("Enter booking number: ");
        bookingNo = keyboard.next();
        System.out.print("Enter room type: ");
        roomType = keyboard.next();

        aBooking = new Booking(title, firstName, lastName, bookingNo, roomType);
    }

    public void printCustomerName() {
        System.out.println(title);
        // ...should perhaps be
        // System.out.println(firstName + " " + lastName);
    }
}

由于您确实创建了Booking实例,因此您可能希望摆脱titlefirstNamelastNamebookingNo和{{1然后将它们放在roomType类中。然后通过Booking依次访问它们......

答案 1 :(得分:3)

执行aBooking = new Booking(...)时,您正在创建一个包含所有这些属性的新Booking对象,并将其存储在aBooking字段中(我猜它是一个字段,因为它没有声明任何地方)。这意味着您有一个包含所有这些属性的aBooking字段(假设Booking构造函数保存参数)。因此,要访问这些字段,请浏览aBooking字段。可能是这样的:

System.out.println(aBooking.getTitle());

或者,如果你没有使用getter(你应该!):

System.out.println(aBooking.title);

一旦离开方法,您在createBooking方法中声明的变量就会停止“存在”。他们无法以任何方式访问(好吧,差不多)。

答案 2 :(得分:0)

您无法访问方法的变量,也无法使用点运算符将方法用作类实例。

createBooking.something是非法的,您可以使用该方法:createBooking()

答案 3 :(得分:0)

我完全不明白你想做什么。但我认为你想要这样的东西:

public String printCustomerName() {
  // This creates the booking object (aBooking)
  createBooking();

  // You can access the firstname lastname like that (I assume that you have a getter method implemented..)
  return aBooking.getFirstName() + " " + aBooking.getLastName();
}

但是你应该将createBooking()移到另一个地方。也许进入构造函数并在那里调用..

答案 4 :(得分:0)

您可能需要考虑将createBooking()方法的返回类型从'void'更改为'Booking',然后最后一行将变为:

private Booking createBooking()
{
    ...
    ...

    return new Booking(title, firstName, lastName, bookingNo, roomType)``
}

之后,您的printCustomerName()可能类似于:

public void printCustomerName()
{
    Booking booking  = createBooking();
    System.out.println (booking.title); // if title is visible, which it probably shouldn't be
    //or
    System.out.println (booking.getTitle()); // if such a method exists...
}