从位于私有方法中的变量读取
我遇到一个小问题,我正在尝试打印位于私有方法中的几个变量的内容。但我只是不断收到“找不到符号”
下面是我试图从中读取数据的代码(包括 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能希望将这些变量作为成员变量 ,然后无需使用
.
运算符即可访问它。然而,由于您确实创建了
Booking
实例,因此您可能需要删除title
、firstName
、lastName
、bookingNo
和roomType
并将它们放入Booking
类中。然后通过aBooking.getTitle()
访问它们等等...You probably want to put these variables as member variables, and then simply access it without using the
.
-operator.Since you do create a
Booking
instance however, you may want to get rid oftitle
,firstName
,lastName
,bookingNo
androomType
and put them in theBooking
class instead. And then access them throughaBooking.getTitle()
and so on...当您执行
aBooking = new Booking(...)
时,您将创建一个包含所有这些属性的新Booking
对象,并将其存储在aBooking
中> 字段(我猜它是一个字段,因为它没有在任何地方声明)。这意味着您有一个 aBooking 字段来保存所有这些属性(假设Booking
构造函数保存参数)。因此,要访问这些字段,您需要通过aBooking
字段。可能是这样的:或者,如果您不使用 getters(您应该!):
一旦您离开该方法,您在 createBooking 方法中声明的变量将停止“存在”。它们无法以任何方式访问(嗯,几乎)。
When you do
aBooking = new Booking(...)
you're creating a newBooking
object with all those attributes and storing it in theaBooking
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 theBooking
constructor saves the parameters). So, to access those fields you go through theaBooking
field. Probably something like this:or, if you're not using getters (you should!):
The variables you declare inside the
createBooking
method stop "existing" once you leave the method. They're not accessible in any way (well, almost).您无法访问方法的变量,也无法使用点运算符将方法用作类实例。
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()
您可能需要考虑将
createBooking()
方法的返回类型从'void'
更改为'Booking'
,然后是最后一行会变成:之后,您的
printCustomerName()
可能看起来像这样:You may want to consider changing the return type of the
createBooking()
method from'void'
to'Booking'
, and then the last line would become:After that, your
printCustomerName()
might look like something like:我完全不明白你想做什么。但我认为你想要这样的东西:
但是 createBooking() 你应该移动到另一个地方。也许进入构造函数并在那里调用它..
I don't understand totally what you wanna do. But i think you want something like this:
But the createBooking() you should move to another place. Maybe into the Constructor and call it there..