简体   繁体   English

如何创建使用另一个类中的变量的Java对象?

[英]How do you create a java object that uses a variable from another class?

How do you create a java object that uses a variable from another class, or calls the entire constructor? 如何创建使用另一个类中的变量的Java对象,或调用整个构造函数?

For example, accountNumber, firstName, lastName, phone are all variables passed in address is comprised of street, city, state, and zip, and has already been created: 例如,accountNumber,firstName,lastName,phone都是在地址中传递的变量,它们由街道,城市,州和邮政编码组成,并且已经创建:

Address address = new Address(street, city, state, zip);

Data is comprised of only megabytes, and has already been created: 数据仅包含兆字节,并且已经创建:

Data data = new Data(megabytes);

This is what I have for the customer object: 这是我为客户对象准备的:

Customer customer = new Customer(accountNumber, firstName, lastName, address, phone, data);

This is supposed to be an "overloaded constructor", but I don't understand what that means. 这应该是“重载的构造函数”,但是我不明白这是什么意思。

This is the constructor I have so far: 这是我到目前为止的构造函数:

public Customer(String accountNumber, String firstName, String lastName, Address address, int phone, Data megabytes)
{
    this.accountNumber = accountNumber;
    this.firstName = firstName; 
    this.lastName = lastName;
    this.address = address; 
    this.phone = phone; 
    this.megabytes= megabytes; 
}

I get the error: 我得到错误:

The constructor Customer(String, String, String, Address, int, Data) is undefined

At a glance everything seems fine. 一切看起来一目了然。 I hoped you saved the file before compiling. 我希望您在编译之前先保存文件。

Since you mentioned that you didn't understand what an overloaded constructor is, I'll try my best to explain that. 由于您提到您不了解重载的构造函数是什么,因此我将尽力说明这一点。

An overloaded constructor has the same constructor name but it differs from other constructors in the following ways - 重载的构造函数具有相同的构造函数名称,但在以下方面与其他构造函数不同:

  1. It has different number of formal arguments 它有不同数量的形式论证
  2. The order of type of formal parameters of the constructor are different 构造函数的形式参数类型的顺序不同

Here is an example - 这是一个例子-

public class Customer {
    private String firstName;
    private String lastName;
    private int phoneNumber;

    public Customer() {
        // default constructor
    }

    public Customer(String firstName) {
        this.firstName = firstName;
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Customer(String firstName, String lastName, int phoneNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
    }

    public Customer(int phoneNumber, String firstName, String lastName) {
        this.phoneNumber = phoneNumber;  
        this.firstName = firstName;
        this.lastName = lastName;
    }

// This is not an overloaded constructor as there is already a constructor of type
// Customer(String, String)

//    public Customer(String lastName, String firstName) {
//        this.lastName = lastName;
//        this.firstName = firstName;
//    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM