如何从Java中的另一个类访问变量?

如何从Java中的另一个类访问变量?,java,Java,我有一个名为Customer的类,customerName、CustomerMail和customerAddress作为变量设置在名为a的对象中。如何在另一个名为BookingConfirmation的类中打印变量customerName、CustomerMail和customerAddress?我试过: System.out.println("Thanks for your booking " + a.getCustomerName()); 以下是Customer类的代码

我有一个名为Customer的类,customerName、CustomerMail和customerAddress作为变量设置在名为a的对象中。如何在另一个名为BookingConfirmation的类中打印变量customerName、CustomerMail和customerAddress?我试过:

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