如何从Java中的另一个类访问变量?
我有一个名为Customer的类,customerName、CustomerMail和customerAddress作为变量设置在名为a的对象中。如何在另一个名为BookingConfirmation的类中打印变量customerName、CustomerMail和customerAddress?我试过:如何从Java中的另一个类访问变量?,java,Java,我有一个名为Customer的类,customerName、CustomerMail和customerAddress作为变量设置在名为a的对象中。如何在另一个名为BookingConfirmation的类中打印变量customerName、CustomerMail和customerAddress?我试过: System.out.println("Thanks for your booking " + a.getCustomerName()); 以下是Customer类的代码
System.out.println("Thanks for your booking " + a.getCustomerName());
以下是Customer类的代码:
public class Customer {
String customerName;
String customerEmail;
String customerAddress;
public static void customerDetails() throws IOException {
Customer a = new Customer();
Scanner seekName = new Scanner(System.in); // Create a Scanner object
System.out.print("Your name: ");
a.customerName = seekName.nextLine(); // Read user input
Scanner seekEmail = new Scanner(System.in); // Create a Scanner object
System.out.print("Your email: ");
a.customerEmail = seekEmail.nextLine(); // Read user input
Scanner seekAddress = new Scanner(System.in); // Create a Scanner object
System.out.print("Your residential address: ");
a.customerAddress = seekAddress.nextLine(); // Read user input
System.out.println("Thanks for your booking " + a.getCustomerName());
System.out.println("Eemail: " + a.getCustomerEmail());
System.out.println("Address: " + a.getCustomerAddress());
System.out.println();
}
public String getCustomerName() {
return customerName;
}
public String getCustomerEmail() {
return customerEmail;
}
public String getCustomerAddress() {
return customerAddress;
}
}
我不确定我是否知道你在问什么,但似乎你可以做到以下几点:
customerDetails()
方法返回a
(创建的客户对象)作为该方法的结果
Customer myCust = new Customer();
myCust.customerDetails();
BookingConfirmation
调用Customer.customerDetails()
,并将返回值保存在局部变量Customer a
中
System.out.println(“感谢您的预订”+a.getCustomerName())代码>在预订确认
代码中
我不确定我是否知道你在问什么,但似乎你可以做到以下几点:
customerDetails()
方法返回a
(创建的客户对象)作为该方法的结果
Customer myCust = new Customer();
myCust.customerDetails();
BookingConfirmation
调用Customer.customerDetails()
,并将返回值保存在局部变量Customer a
中
System.out.println(“感谢您的预订”+a.getCustomerName())代码>在预订确认
代码中
如果只想从类外部访问属性的值,那么需要为要检索的每个属性创建getter 如果要从类外部修改这些值,则需要创建一个setter getter和setter只是允许您访问外部类的属性的方法 如果您拥有类
Customer
,并希望从外部访问和更改其名称,则需要创建两个方法
//declare the property
private String name;
//getter
public String getName(){
return this.name;
}
//setter public void setName(String newName){
this.name = newName;
}
//You can then instantiate a Customer object anywhere else and have access to those //properties
Customer cust = new Customer();
cust.setName("Mark")
System.out.println("Oh hi " + cust.getName());
//output "Oh hi Mark"
阅读更多关于
此外,最佳实践提示:实例变量应始终声明为private
,以帮助封装。如果Java中没有为实例变量提供访问修饰符,则默认为default
修饰符,这使得同一个包中的每个类都可以访问该变量
编辑:
您的具体错误是,您正在customerDetails
方法中创建一个新的Customer
对象,并且您没有返回此Customer
对象。因此,一旦执行了该方法,您在其中创建的Customer
对象就会被销毁,因为没有对它的进一步引用
你要么需要
方法1:将customerDetails方法的返回类型从void
更改为Customer
,并在末尾添加returna
语句,然后只需从booking类实例化一个Customer对象,如下所示
public Customer customerDetails(){
Customer a = new Customer();
//your logic to set all of the properties
return a;
}
在您的预订课上
Customer myCust = new Customer();
myCust = myCust.customerDetails();
我不喜欢这种方法,因为正如您所看到的,您只是创建了一个空对象,然后重新分配给它。您也可以在public
之后添加static
关键字,这样您就可以在没有实例化类的对象的情况下调用它,如下所示
public Customer customerDetails(){
Customer a = new Customer();
//your logic to set all of the properties
return a;
}
预约班
Customer myCust = Customer.customerDetails();
方法2:从customerDetails中删除Customer a=new Customer()
,只需使用this.name=sc.nextLine()
设置调用此方法的任何实例的名称
然后在bookings类上,实例化一个新的Customer对象并调用该方法
Customer myCust = new Customer();
myCust.customerDetails();
如果只想从类外部访问属性的值,那么需要为要检索的每个属性创建getter 如果要从类外部修改这些值,则需要创建一个setter getter和setter只是允许您访问外部类的属性的方法 如果您拥有类
Customer
,并希望从外部访问和更改其名称,则需要创建两个方法
//declare the property
private String name;
//getter
public String getName(){
return this.name;
}
//setter public void setName(String newName){
this.name = newName;
}
//You can then instantiate a Customer object anywhere else and have access to those //properties
Customer cust = new Customer();
cust.setName("Mark")
System.out.println("Oh hi " + cust.getName());
//output "Oh hi Mark"
阅读更多关于
此外,最佳实践提示:实例变量应始终声明为private
,以帮助封装。如果Java中没有为实例变量提供访问修饰符,则默认为default
修饰符,这使得同一个包中的每个类都可以访问该变量
编辑:
您的具体错误是,您正在customerDetails
方法中创建一个新的Customer
对象,并且您没有返回此Customer
对象。因此,一旦执行了该方法,您在其中创建的Customer
对象就会被销毁,因为没有对它的进一步引用
你要么需要
方法1:将customerDetails方法的返回类型从void
更改为Customer
,并在末尾添加returna
语句,然后只需从booking类实例化一个Customer对象,如下所示
public Customer customerDetails(){
Customer a = new Customer();
//your logic to set all of the properties
return a;
}
在您的预订课上
Customer myCust = new Customer();
myCust = myCust.customerDetails();
我不喜欢这种方法,因为正如您所看到的,您只是创建了一个空对象,然后重新分配给它。您也可以在public
之后添加static
关键字,这样您就可以在没有实例化类的对象的情况下调用它,如下所示
public Customer customerDetails(){
Customer a = new Customer();
//your logic to set all of the properties
return a;
}
预约班
Customer myCust = Customer.customerDetails();
方法2:从customerDetails中删除Customer a=new Customer()
,只需使用this.name=sc.nextLine()
设置调用此方法的任何实例的名称
然后在bookings类上,实例化一个新的Customer对象并调用该方法
Customer myCust = new Customer();
myCust.customerDetails();
理论上,您不能从局部变量的作用域之外访问它(在本例中为
a
)。在您的示例中,对象a