Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用对象时,在创建新对象之后,他无法从主界面访问对象_Java_Object - Fatal编程技术网

在java中使用对象时,在创建新对象之后,他无法从主界面访问对象

在java中使用对象时,在创建新对象之后,他无法从主界面访问对象,java,object,Java,Object,大体上,我写了这两个声明: Account newAccount = new Account(name); newAccount.addNewProduct(); 这是Account中的构造函数代码和属性: private final int MAX_PRODUCTS = 50; //Assumes no more than 50 products per customer private String name; //Name of the customer private i

大体上,我写了这两个声明:

Account newAccount = new Account(name);
newAccount.addNewProduct();
这是Account中的构造函数代码和属性:

private final int MAX_PRODUCTS = 50; //Assumes no more than 50 products per customer
private String name;        //Name of the customer
private int sum;            //Sum amount of the purchase
private Product[] productList;      //List of products for a customer 
private int productCounter;         //Counter for number of products

public Account(String name)
{
    Product[] productList = new Product[MAX_PRODUCTS]; //New empty list of products of the customer }
    productCounter = 0;
    sum = 0;
    name = name;
}
我不知道为什么,但用这种方法:

public void addNewProduct()
{   
    System.out.println("Name is: " + this.name);
    productList[productCounter] = new Product();
    productCounter++;
} 
虽然我在控制台中输入了一个名称,但它将名称打印为null。。。
这就像不保存我创建的对象newAccount的属性一样。为什么?

name=name
不起作用。您需要使用
this.name=name


这是因为当您使用
name
时,您引用了构造函数中的参数;使用
this.name
时,您将引用该字段。

name=name
将不起作用。您需要使用
this.name=name


这是因为当您使用
name
时,您引用了构造函数中的参数;当您使用
这个.name
时,您指的是该字段。

正如Anubian Noob所说:

name=name将不起作用,因为您正在将相同的值分配给相同的变量

如果您收到name=“John”,您正在做的是:

约翰=约翰

这就是为什么您需要使用它,当您使用this.name=name时,您正在将收到的值从类传递给局部变量

所以如果你有这个:

private final int MAX_PRODUCTS = 50; //Assumes no more than 50 products per customer
private String name;        //Name of the customer
private int sum;            //Sum amount of the purchase
private Product[] productList;      //List of products for a customer 
private int productCounter;         //Counter for number of products

public Account(String name)
{
    Product[] productList = new Product[MAX_PRODUCTS];
    productCounter = 0;
    sum = 0;
    this.name = name;
}

您的值将存储在名为name的私有变量中。

正如Anubian Noob所说:

name=name将不起作用,因为您正在将相同的值分配给相同的变量

如果您收到name=“John”,您正在做的是:

约翰=约翰

这就是为什么您需要使用它,当您使用this.name=name时,您正在将收到的值从类传递给局部变量

所以如果你有这个:

private final int MAX_PRODUCTS = 50; //Assumes no more than 50 products per customer
private String name;        //Name of the customer
private int sum;            //Sum amount of the purchase
private Product[] productList;      //List of products for a customer 
private int productCounter;         //Counter for number of products

public Account(String name)
{
    Product[] productList = new Product[MAX_PRODUCTS];
    productCounter = 0;
    sum = 0;
    this.name = name;
}

您的值将存储在名为name的私有变量中。

ohhh因此,如果我将在方法declaration:public Account(字符串newName)中写入的话。然后name=newName。它会好吗?哦,如果我将写在方法decellation:publiccount(stringnewname)中。然后name=newName。会没事的?这又给另一个答案增加了什么?如果你有建议;在那里注释它,否则这是多余的。这会给另一个答案增加什么?如果你有建议;在那里注释它,否则这是多余的。