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

发布于 2024-09-30 18:31:26 字数 814 浏览 7 评论 0原文

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

下面是我试图从中读取数据的代码(包括 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);
}

I am having a small problem I am trying to print the contents of a couple of variables which are located in a a private method. but I simply keep getting 'Can Not Find Symbol'

Below is the code that I am trying to read the data from (including the println) also I am very new to 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晌融 2024-10-07 18:31:26

您可能希望将这些变量作为成员变量 ,然后无需使用 . 运算符即可访问它。

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 实例,因此您可能需要删除 titlefirstNamelastNamebookingNoroomType 并将它们放入 Booking 类中。然后通过 aBooking.getTitle() 访问它们等等...

You probably want to put these variables as member variables, and then simply access it without using the .-operator.

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);
    }
}

Since you do create a Booking instance however, you may want to get rid of title, firstName, lastName, bookingNo and roomType and put them in the Booking class instead. And then access them through aBooking.getTitle() and so on...

堇年纸鸢 2024-10-07 18:31:26

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

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

或者,如果您不使用 getters(您应该!):

System.out.println(aBooking.title);

一旦您离开该方法,您在 createBooking 方法中声明的变量将停止“存在”。它们无法以任何方式访问(嗯,几乎)。

When you do aBooking = new Booking(...) you're creating a new Booking object with all those attributes and storing it in the aBooking field (I'm guessing it's a field since it's not declared anywhere). This means you have a aBooking field that holds all those attributes (assuming the Booking constructor saves the parameters). So, to access those fields you go through the aBooking field. Probably something like this:

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

or, if you're not using getters (you should!):

System.out.println(aBooking.title);

The variables you declare inside the createBooking method stop "existing" once you leave the method. They're not accessible in any way (well, almost).

∞琼窗梦回ˉ 2024-10-07 18:31:26

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

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

You cannot access to variable of a method and cannot use a method as a class instance using a dot operator.

createBooking.something is illegal , you can use that method: createBooking()

日久见人心 2024-10-07 18:31:26

您可能需要考虑将 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...
}

You may want to consider changing the return type of the createBooking() method from 'void' to 'Booking', and then the last line would become:

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

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

After that, your printCustomerName() might look like something like:

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...
}
你又不是我 2024-10-07 18:31:26

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

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() 你应该移动到另一个地方。也许进入构造函数并在那里调用它..

I don't understand totally what you wanna do. But i think you want something like this:

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();
}

But the createBooking() you should move to another place. Maybe into the Constructor and call it there..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文