在另一个方法中从另一个类调用Collections.frequency方法中的对象

时间:2018-02-14 12:41:10

标签: java

//这是我创建对象A的类App

App.java

Product A = new Product(name:"ThisIsAName",price: 3);

//这是类注册,我希望在'ThisIsWhereIWantToCallA'中从类App调用A,以便能够计算A在特定客户名为Basket的数组列表中出现的次数。

但是,如果我只是将A放在ThisIsWhereIWantToCallA上,我得到:无法解析符号'A'。

Register.java

public void calculateAmountPerProduct(Customer customer) {

   int amountOfA = Collections.frequency(customer.getBasket(), ThisIsWhereIWantToCallA);
   System.out.println("Frequency of A:" + amountOfA);

如何从App.java中正确调用A?

1 个答案:

答案 0 :(得分:0)

App类:

public class App {
    private Product product;

    public void setProduct(Product product) {
        this.product = product;
    }

    public Product getProduct() {
        return product;
    }
}

注册课程:

public class Register {
    public void calculateAmountPerProduct(Customer customer, App app) {
        int amountOfA = Collections.frequency(customer.getBasket(), app.getProduct());
        System.out.println("Frequency of A:" + amountOfA);
    }

    public static void main(String... args) {
        App app = new App();
        Register register = new Register();
        Customer customer = new Customer();

        app.setProduct(new Product("ThisIsAName", 3));

        register.calculateAmountPerProduct(customer, app);
    }
}