gpt4 book ai didi

java - 如何重写java中的抽象方法?

转载 作者:行者123 更新时间:2023-12-01 06:01:12 25 4
gpt4 key购买 nike

此代码的目的是查找人们希望购买的商品数量、这些商品的价格以及当前的促销情况。对于买三送一的销售,它看起来像

1 项为 5.0;折扣为0

2 项为 5.0;折扣为0

3 项为 5.0;折扣5.0

4 项为 5.0;折扣5.0

该类扩展了一个抽象类,其抽象方法是computeDiscount()

但是,我不知道如何使这个方法发挥作用,因为它不会将其视为静态,但如果该方法不是静态的,那么我不能在我的代码中使用它!

我不知道该怎么办,我迫切需要帮助

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;

}
}

最佳答案

如果你想用 abstraction 做任何事情,你不能使用静态方法。在这里,最好使用实例变量结合工厂方法来完成所有初始化操作。

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 中的参数,因为它们(据我所知)只是引用已经能够使用的字段。

关于java - 如何重写java中的抽象方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com