构造函数 Game(String, int, int, double, String) 未定义

问题描述 投票:0回答:1

所以我在java中创建一个存储,由于某种原因,在进行类继承时,它说我的构造函数未定义。

下面是我的代码

public class Store {
    private String name;
    private ArrayList<Item> itemArray;
    private int discount;

    public Store(String initName, int initDiscount) {
        name = initName;
        discount = initDiscount;
        itemArray = new ArrayList<Item>();
        Game item1 = new Game("Pokemon", 2000, 200, 4.5, "Nintendo");
        itemArray.add(item1);
    }
}

public class Item {
    public String name;
    public int price;
    public boolean taxable;
    public String category;
    public int inventory;

    public Item(String initName, int initPrice, boolean initTaxable, String initCategory, int initInventory) {
        name = initName;
        price = initPrice;
        taxable = initTaxable;
        category = initCategory;
        inventory = initInventory;
    }

    public void description() {
        System.out.println("This is a " + name);
        System.out.println("The base price is " + displayPrice());
        if (taxable) {
            System.out.println("This item is taxed");
        } else {
            System.out.println("This item is not taxed");
        }
        System.out.println("This item is a " + category);
        System.out.println("There are " + inventory + " of this item left");
    }
    //Gets the price of the item before discounts(The price is in cents)
    public int getBasePrice() {
        return price;
    }
    //Gets the price of the item after discounts are applied the percentages should be in decimal form ex. .3 is 30% off
    public int getCurrentPrice(int discount) {
        int percentOfFullPrice = 100 - discount;
        double percentOfFullPriceDouble = percentOfFullPrice / 100.0;
        double finalPrice = percentOfFullPriceDouble * price;
        int finalIntPrice = (int) finalPrice;
        return finalIntPrice/100;
    }
    //Sets the new price of the item
    public void setPrice(int newPrice) {
        price = newPrice;
    }
    //Displays the price as a string
    public String displayPrice() {
        return "$" + (float) price / 100;
    }
    //Gets the inventory of the item
    public int getInventory() {
        return inventory;
    }

    //Adds to the inventory of the item
    public int addInventory(int additionalInventory) {
        inventory += additionalInventory;
        return inventory;
    }
}

public class Game extends Item {
    public float reviews;
    public String companyBy;

    public Game(String initGameName, int initPrice, int initInventory, float initReviews, String initCompanyBy) {
        super(initGameName, initPrice, true, "Video Game", initInventory);
        reviews = initReviews;
        companyBy = initCompanyBy;
    }

    public void getReviews() {
        System.out.println("This video game has " + reviews + " out of 5 stars");
    }
    
}

我尝试使用将传递给超类的参数来初始化一个对象,但是,它似乎未定义。如果我简单地使用 Item 类初始化某些东西,它可以工作,但我想要不同的子集。

java constructor compiler-errors
1个回答
0
投票

您的实际构造函数是

Game(String, int, int, float, String)
,请注意 float,同时您为其提供 double -
new Game("Pokemon", 2000, 200, 4.5, "Nintendo");
。作为文字的值
4.5
的类型为 double。要使其成为浮动,您需要像这样使用它 -
4.5f
4.5F

new Game("Pokemon", 2000, 200, 4.5F, "Nintendo");

检查浮点文字、浮点文字、双精度文字以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.