如何从另一个类传递一个类引用?

huangapple go评论100阅读模式
英文:

How do I pass a class reference from another class?

问题

我正在构建一个基本的初学者Java程序,但我一直遇到NullPointerException错误,似乎无法解决这个问题。

我有两个类 - User(用户)和Cart(购物车)。

User类包括以下代码中的Cart类:

package eCommerceApp;

import java.util.List;
import java.util.Collections;
import java.util.Comparator;

public class User extends Person {

    public User(String name, String surname, int budget) {
        super(name, surname, budget);
    }

    public Cart myCart;

    public String getReceipt(List<Product> products) {

        int sum = 0;

        for (Product product : products) {

            sum += product.getPrice();
        }
        return "Your total bill is " + sum + " rsd.";
    }

    public String getMostExpensiveItem(List<Product> products) {

        Product product = Collections.max(products, Comparator.comparing(s -> s.getPrice()));

        return "The most expensive item in your shopping cart is " + product.getName() + ".";
    }

    public String getCheapestItem(List<Product> products) {

        Product product = Collections.min(products, Comparator.comparing(s -> s.getPrice()));
        return "The cheapest item in your shopping cart is " + product.getName() + ".";
    }
}

而Cart类如下:

package eCommerceApp;

import java.util.List;

public class Cart {

    List<Product> products;

    public Cart(List<Product> products) {

        this.products = products;
    }

    public String getProducts() {
        return this.products.toString();
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
}

现在,当我在主类中输入**User.myCart.setProducts(list)**以将产品列表设置为我的User的购物车时,我会收到NullPointerException错误。我该如何解决这个问题?

英文:

I'm building a basic beginner Java program but I keep getting the NullPointerException error and I can't seem to solve the problem.

I have two classes - User and Cart.

The User class includes the Cart class with the following code :

package eCommerceApp;

import java.util.List;
import java.util.Collections;
import java.util.Comparator;

public class User extends Person {

    public User(String name, String surname, int budget) {
        super(name, surname, budget);
    }

    public Cart myCart;

    public String getReceipt(List&lt;Product&gt; products) {

        int sum = 0;

        for (Product product : products) {

            sum += product.getPrice();
        }
        return &quot;Your total bill is &quot; + sum + &quot; rsd.&quot;;
    }

    public String getMostExpensiveItem(List&lt;Product&gt; products) {

        Product product = Collections.max(products, Comparator.comparing(s -&gt; s.getPrice()));

        return &quot;The most expensive item in your shopping cart is &quot; + product.getName() + &quot;.&quot;;

    }

    public String getCheapestItem(List&lt;Product&gt; products) {

        Product product = Collections.min(products, Comparator.comparing(s -&gt; s.getPrice()));
        return &quot;The cheapest item in your shopping cart is &quot; + product.getName() + &quot;.&quot;;

    }

}

And the Cart Class :


import java.util.List;

public class Cart {

    List&lt;Product&gt; products;

    public Cart(List products) {

        this.products = products;
    }

    public String getProducts() {
        return this.products.toString();
    }

    public void setProducts(List products) {
        this.products = products;
    }

}

Now, when I want to set a list of products to my User's Cart by typing User.myCart.setProducts(list) in the main class, I get the NullPointerException error. How can I solve this?

答案1

得分: 1

为了在其他类中使用类,您需要像这样创建一个实例:

public Cart myCart = new Cart();

除非您的整个类都是静态的,这是可行的方式。但通常不推荐始终将类设为静态。

英文:

To use classes in other classes, you'll need to make an instance of them like so:

public Cart myCart = new Cart();

Unless your entire class is static, this is the way to go. But it is generally not good practice to make your classes static all the time.

huangapple
  • 本文由 发表于 2020年8月18日 09:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63460533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定