簡體   English   中英

如何覆蓋 java 中的抽象方法?

[英]How do I override an abstract method in java?

此代碼的目的是查找一個人希望購買的物品數量、這些物品的價格以及當前的銷售情況。 對於買三送一的免費銷售,它看起來像

5.0 的 1 項; 折扣為 0

5.0 的 2 項; 折扣為 0

3 項 5.0; 折扣為 5.0

4 項 5.0; 折扣為 5.0

class 擴展了一個抽象 class,其抽象方法是 computeDiscount()

但是,我不知道如何制作此方法 function,因為它不會將其除為 static,但如果該方法不是 static,則無法在我的代碼中使用它!

我不知道該怎么辦,我迫切需要幫助

package homeFolder;

import java.util.Scanner;

public class BuyNItemsGetOneFree extends DiscountPolicy{

        static Scanner input = new Scanner(System.in);
        static double itemCost;
        static int count = count();
        static int n = getN();
        static double discount = 0;

        public static void main(String[] args) {

            itemCost = itemCost();

            for(int i = 1; i <= count; i ++) {

                discount = computeDiscount(i, itemCost);

                System.out.println(i + " items at " + itemCost +
                                    "; discount is " + discount);

            }

        }

        public static double itemCost() {

            System.out.print("Enter the cost of the item: ");
            itemCost = input.nextDouble();

            return itemCost;

        }

        public static int count() {

            System.out.print("How many items are you going to buy: ");
            count = input.nextInt();

            return count;

        }

        public static int getN() {

            System.out.print("How many items must you buy till you got one free? ");
            n = input.nextInt();

            return n;

        }

        public double computeDiscount(int count, double itemCost) {

            double discount = 0;

            if((count % n) == 0)
                discount += itemCost;

            return discount;

        }
    }

Spencer 使您的所有方法 static 並且您的主要方法也缺少 static。 它應該是這樣的public static void main(String [] args)

如果你想用抽象做任何事情,你不能使用 static 方法。 在這里,最好使用實例變量結合工廠方法來完成所有的初始化動作。

package homeFolder;

import java.util.Scanner;

public class BuyNItemsGetOneFree extends DiscountPolicy {

    // Below are your fields, or instance variables. Notice the lack of static.
    final double itemCost;
    final int count;
    final int n;

    public static void main(String[] args) {

        // Now you have an instance to work with.
        BuyNItemsGetOneFree sale = newInstance();

        for(int i = 1; i <= sale.count; i ++) {

            double discount = sale.computeDiscount(i, itemCost);

            System.out.println(i + " items at " + sale.itemCost +
                                "; discount is " + discount);

        }

    }

    // Only the factory method can access this.
    private BuyNItemsGetOneFree (double itemCost, int count, int n) {
        this.itemCost = itemCost;
        this.count = count;
        this.n = n;
    }

    public static BuyNItemsGetOneFree newInstance() {

        // The scanner really only needs to be used here.
        Scanner input = new Scanner(System.in);

        // Initilizes itemCost
        System.out.print("Enter the cost of the item: ");
        double itemCost = input.nextDouble();

        // Initilizes count
        System.out.print("How many items are you going to buy: ");
        int count = input.nextInt();

        // Initilizes n
        System.out.print("How many items must you buy till you got one free? ");
        int n = input.nextInt();

        // Constructs and returns
        return new BuyNItemsGetOneFree(itemCost, count, n);
    }

    @Override // Always add this when overriding a method.
    public double computeDiscount(int count, double itemCost) {

        double discount = 0;

        if((count % n) == 0)
            discount += itemCost;

        return discount;
    }
}

如果您想更進一步,則不需要computeDiscount中的參數,因為它們(據我所見)只是參考已經能夠使用的字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM