如何在另一个类中引用对象

时间:2019-05-27 17:26:32

标签: java reference

我是Java的初学者。我必须做购物车。 但我坚持参考。

请帮助我!

我试图在Customer类中使私有成员变量ShoppingCart。 但是我认为这不是正确的方法。

Customer.java

public class Customer {
    private String id;
    private String firstName;
    private String lastName;
    private ShoppingCart s;

    public Customer(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        s = new ShoppingCart();
    }

    @Override
    public String toString() {
        return ("Customer ID is: " + this.id + "\n" + "Customer's name is: "
                 + this.firstName + " " + this.lastName + "\n\n" 
                + s.toString());

    }
}

Test.java

public class Test {
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Customer c1 = new Customer("12345", "David", "Smith");

        //This sentence doesn't work because of s.
        c1.s.addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);



    }

}

ShoppingCart.java

public class ShoppingCart {
    private int itemCount;
    private double totalPrice;
    private static int capacity;
    private Item[] cart;
    public ShoppingCart()
    {       
        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    public void addBooktoCart(String title, String description, double price, int pageCount) {

        if (itemCount < 5) {

        Item item = new Book(title, description, price, pageCount);
        totalPrice += price;
        cart[itemCount] = item;
        itemCount += 1;       
        } 
        else {
            System.out.println("The maximum number that you can input is 5." +
                     "You cannot add item anymore");
        }

    }

}

我想调用Test.java中ShoppingCart类中的addBooktoCart。 但这是行不通的。如果我叫addBooktoCart怎么办?并且如果还有另一个问题。请让我知道!

2 个答案:

答案 0 :(得分:1)

您绝对有权使用private。只是不要直接引用字段。相反,请使用get/set方法。

c1.getS().addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);

顺便说一句,请仔细选择您的字段名称。 s毫无意义。

答案 1 :(得分:0)

在Java中,按照惯例,除非我们有任何特定的理由不这样做,否则我们应将所有变量都保留为私有。因此,在您的情况下,应将ShoppingCart保留为Customer类中的私有字段。此外,在命名变量时,java遵循命名约定

引用doc

  

变量名称区分大小写。变量的名称可以是任何合法的   标识符-无限长度的Unicode字母和   以字母,美元符号“ $”或数字开头的数字   下划线字符“ ”。然而,惯例总是要开始   您的变量名要带字母,而不是“ $”或“ ”。此外,   按照惯例,绝不使用美元符号字符。你可以   查找某些情况下,自动生成的名称将包含   美元符号,但您的变量名称应始终避免使用它。一种   下划线字符存在类似的约定;虽然是   从技术上讲,以“ _”开头的变量名称是合法的,这   不鼓励实践。不允许使用空格。

对于您的情况,您可以为ShoppingCart提供setter和getter方法以获取对象,然后在进行如下操作后将其设置为实例:

Customer.java

public class Customer {
    private String id;
    private String firstName;
    private String lastName;
    private ShoppingCart shoppingCart;

    public ShoppingCart getShoppingCart() {
      return shoppingCart;
    }

    public ShoppingCart setShoppingCart(ShoppingCart shoppingCart) {
      this.shoppingCart = shoppingCart;
    }

    public Customer(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        s = new ShoppingCart();
    }

    @Override
    public String toString() {
        return ("Customer ID is: " + this.id + "\n" + "Customer's name is: "
                 + this.firstName + " " + this.lastName + "\n\n" 
                + s.toString());

    }
}

Test.java

public class Test {
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ShoppingCart shoppingCart = new ShoppingCart();

        Customer customer = new Customer("12345", "David", "Smith");

        //This sentence doesn't work because of s.
        shoppingCart.addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);
        customer.setShoppingCart(shoppingCart);
    }

}