Java对象和引用:为什么这会起作用; mainApp.getProductsData()。除去(将selectedItem)

时间:2017-10-29 17:59:22

标签: java

我对Java很新。我在其他开发人员的Java代码中看到了很多这样的东西,我不确定Java是否跟踪它创建的每个对象。假设我在JavaScript中工作,我写了这样的东西;

//I could not do the following in JavaScript
var myProduct = new Product("Product name", "SKU", "20", "20.93", "12.5");
productCollection.getProducts().add (myProduct);

//Now I can access my product. This will return null because the product does not exist although it was added.
var theProductThatIAddedWas = productCollection.getProducts().findWhere("title", 'Product name');

//And then do this to remove my product
this.productCollection.getProducts().remove(myProduct);
//This product has been detached but I can still remove it and Java will be able to recognize it.

这不起作用,因为我必须在使用它们之前先将产品存储在变量中,并且一旦我对产品进行了更改,我就必须用新产品替换现有产品。有类似的东西;

var products = this.productCollection.getProducts();
//Make changes and then.
this.productCollection.setProducts(products)

我认为这在C#和C ++中非常相似,尽管我暂时还没有触及它们。我现在广泛使用java。我很惊讶我能写; mainApp.getProductsData().remove(selectedItem)然后当我使用mainApp.getProductsData()再次访问产品时,产品已被删除。我只是在这里问,因为我不认为我能够找到这方面的文档或教程,因为我不知道它叫什么或它是如何工作的。另一个例子;

class MainProgram {
    setProduct (product){
        this.product = product;
    }

    getProdut (){
        return this.product;
    }
}

var product = {
    tile: "Product name",
    sku: "SKU",
    qty: "20",
    price: "20.93",
    salePrice: "12.5"
}

MainProgram.setProduct(product);

if (product==MainProgram.getProduct()){
    //Show return true but won't
}

0 个答案:

没有答案