我需要帮助理解抽象类之间的关系
我有一项任务,不太确定从哪里开始。这就是我应该做的。
- 创建一个抽象类 DiscountPolicy。它将有一个抽象方法computeDiscount,该方法将返回购买给定数量的单个商品的折扣。该方法有两个参数,count(int)和itemCost(float)。
- 从Discount Policy派生出一个类BulkDiscount。它将有一个构造函数,该构造函数有两个参数:最小值和百分比。它将定义一个方法computeDiscount,以便如果购买的商品数量超过最小值,则折扣为该类别的百分比。 ComputeDiscount 将返回总折扣。
- 从 DiscountPolicy 派生类 BuyNItemsGetOneFree。该类将有一个具有单个参数 n 的构造函数。此外,该类将定义方法computeDiscount,以便第n 个项目都是免费的。例如:
- 如果 n 为 3,且商品成本为 10 美元。前 2 件商品没有折扣。第 3 – 5 件商品有 10 美元的折扣,第 6 件商品有 20 美元的折扣,等等。
- 对于 BuyNItemsGetOneFree –computeDiscount 方法将接收购买的总商品和商品的成本,并返回总折扣(如果适用) 。
- 在主程序中,显示computeDiscount 方法适用于BulkDiscount 和BuyNItemsGetOneFree 类。
这就是我开始设置它的方式。我想让我的方法和参数位于正确的位置,并且想知道在哪里定义老师希望我传递的参数。
public class Ex1012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DiscountPolicy bulk = new BulkDiscount();
System.out.println();
DiscountPolicy bngo = new BuyNItemsGetOneFree();
}
}
public abstract class DiscountPolicy {
abstract void computeDiscount(int count, float itemCost){
return discount;
}
}
public class BuyNItemsGetOneFree extends DiscountPolicy {
BuyNItemsGetOneFree() {
}
BuyNItemsGetOneFree(int n){
DiscountPolicy.computeDiscount(int count, float itemCost);
//set n to a variable here??
//calculations go here
//Where to set count and itemCost??
}
}
public class BulkDiscount extends DiscountPolicy {
public BulkDiscount(int minimum, float percent){
if (quantity > minimum){
super.ComputeDiscount(int count, float itemCost);
//calculations go here
//Where to define count, itemCost, minimum, and percent??
}
}
}
我只是担心类和参数本身之间的关系,因为一旦我有多个这样的类,我就会感到困惑。任何见解将不胜感激。谢谢!
I have an assignment that I'm not quite sure where to start. This is what I'm supposed to do.
- Create an abstract class DiscountPolicy. It will have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count (int) and itemCost (float)
- Derive a class BulkDiscount from Discount Policy. It will have a constructor that has two parameters, minimum and percent. It will define a method computeDiscount so that if the quantity purchased of an item is more than the minimum, the discount is the percent for the class. ComputeDiscount will return the total discount.
- Derive a class BuyNItemsGetOneFree from DiscountPolicy. The class will have a constructor that has a single parameter n. In addition, the class will define the method computeDiscount so that every nth item is free. For example:
- If n is 3 and the item cost is $10. There is no discount for the first 2 items. There is a $10 discount for items 3 – 5, there is a $20 discount for the 6th item, etc.
- For BuyNItemsGetOneFree – the computeDiscount method will receive the total items bought and the cost for an item, and will return the total discount if applicable.
- In your main program, show that the computeDiscount method works for the BulkDiscount and BuyNItemsGetOneFree classes.
This is how I began to set it up. I want to make my methods and parameters are in the correct locations and am wondering where I define the parameters that my teacher wants me to pass.
public class Ex1012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DiscountPolicy bulk = new BulkDiscount();
System.out.println();
DiscountPolicy bngo = new BuyNItemsGetOneFree();
}
}
public abstract class DiscountPolicy {
abstract void computeDiscount(int count, float itemCost){
return discount;
}
}
public class BuyNItemsGetOneFree extends DiscountPolicy {
BuyNItemsGetOneFree() {
}
BuyNItemsGetOneFree(int n){
DiscountPolicy.computeDiscount(int count, float itemCost);
//set n to a variable here??
//calculations go here
//Where to set count and itemCost??
}
}
public class BulkDiscount extends DiscountPolicy {
public BulkDiscount(int minimum, float percent){
if (quantity > minimum){
super.ComputeDiscount(int count, float itemCost);
//calculations go here
//Where to define count, itemCost, minimum, and percent??
}
}
}
I'm simply worried about the relationship between the classes and the parameters themselves because I get confused once I have multiple classes such as these. Any insight would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
抽象方法可能没有主体,因此您对computeDiscount(...)的定义应该是:
在扩展抽象类的每个具体类中,您必须实现该方法。一般来说,抽象方法的行为在某种程度上类似于接口中定义的方法(它们被声明但没有默认实现),但仍然存在差异(也可以是受保护的或包私有的,只能由子类等实现)。
在大多数情况下,您有一个抽象类,它提供一些默认逻辑,只需要子类填充一些取决于具体实现的“漏洞”。
因此,基本上,您将参数存储为
BuyNItemsGetOneFree
和BulkDiscount
作为实例变量,并在调用computeDiscount(...)
时使用它们。您在构造函数中调用它,这很可能是错误的地方。我想您的 main 应该直接调用您创建的对象上的方法,例如请注意,您的computeDiscount(...)方法应该根据您的分配返回一个值:
编辑:
正如我上面所说,您不“设置”(存储)它们,而仅将它们用于计算。
Abstract methods may not have a body, so your definition of
computeDiscount(...)
should be:In each of the concrete classes that extend the abstract class, you'd then have to implement that method. Generally, abstract methods behave like methods defined in interfaces to some extent (they are declared but without a default implementation), yet still have differences (can be protected or package private as well, can only be implemented by subclasses etc.).
In most cases you have an abstract class that provides some default logic and just requires the subclasses to fill in some "holes" that depend on the concrete implementation.
So basically, you store the parameters to
BuyNItemsGetOneFree
andBulkDiscount
as instance variables and use them whencomputeDiscount(...)
is called. You're calling it in the constructor, which is most likely the wrong place. I guess your main should call the method on the objects you create directly, e.g.Note that your
computeDiscount(...)
method should return a value, according to your assignment:Edit:
As I said above, you don't "set" (store) them, but use them for the calculation only.
您需要回顾如何定义抽象方法。抽象方法没有主体“{}”。它应该在子类中定义。折扣的计算一般是在基类调用抽象方法时由子类进行。
http://download.oracle.com/javase/tutorial/java/IandI /abstract.html
You need to review how to define abstract methods. An abstract method does not have a body "{}". It should be defined in sub-classes. The sub-classes do the calculation of the discount generally when the base class calls the abstract method.
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html
首先,抽象方法没有主体。
其次,因为它应该计算折扣,所以它应该返回它,所以computeDiscount不能为空,它应该在你的情况下返回浮点数。
所以 DiscountPolicy 应该看起来像
此外,你不能使用
computeDiscount方法不是静态方法,它甚至不是具体的。
请注意,java 是区分大小写的语言,因此您应该使用声明它们的内容,computeDiscount 和 ComputeDiscount 是两个不同的东西。
Firstly, abstract methods do not have body.
Secondly as it should compute discount it should return it so computeDiscount canot be void it should in your case return float.
So DiscountPolicy should look like
Furthermore you can not use
as computeDiscount method is not static method, it is even not concrete.
Just a note, java is case sensitive language so you should be using stuff as you declared them, computeDiscount and ComputeDiscount are two different things.
DiscountPolicy
是一个抽象基类。在您的情况下,这给出了表示某种折扣类型的类的结构:所有此类类都应该有一个方法computeDiscount,并且此方法将根据各自的策略计算折扣。这是代表 DiscountPolicy 的所有类都必须遵循的契约。DiscountPolicy
本身没有给出任何计算折扣的逻辑(没有“默认策略”)。每个类都必须提供自己的逻辑。您可以通过将computeDiscount
方法设为abstract
来强制执行它。java中的抽象方法没有任何主体。这只是签名:只是合约(结构),没有实现。
因此,DiscountPolicy 类中的computeDiscount 应该类似于(请注意签名本身末尾的
;
。也没有{}
):此外,此方法将返回购买给定数量的单个商品的折扣,因此返回类型应为
float
而不是void
BuyNItemsGetOneFree< /代码> 和
BulkDiscount
类是DiscountPolicy
的子类,应该实现computeDiscount
方法。两个子类的computeDiscount方法中的逻辑会有所不同,基于批量折扣和买n送1的折扣计算逻辑(这个逻辑在练习中给出)。您可以将它们测试为(在
Ex1012
的main
方法中):DiscountPolicy
is a base abstract class. This, in your case, gives a structure for a class that represents some type of Discount: all such classes should have a method computeDiscount, and this method would calculate the discount based on the respective policies. This is a contract that all classes representing a DiscountPolicy has to follow.DiscountPolicy
itself does not give any logic for calculating a discount (no "default policy"). Every class has to provide their own logic. You enforce it by making thecomputeDiscount
methodabstract
.An abstract method in java does not have any body. It is just the signature: just the contract (structure) and no implementation.
So the computeDiscount in your DiscountPolicy class should look like (Note the
;
at the end of the signature itself. Also no{}
) :Also, this method will return the discount for the purchase of a given number of a single item, so the return type should be a
float
and notvoid
The
BuyNItemsGetOneFree
andBulkDiscount
classes, being subclasses ofDiscountPolicy
should implement thecomputeDiscount
method. The logic in thecomputeDiscount
methods of the two sub classes will be different, based on the discount calculation logic for bulk discount and the buy n get 1 free (this logic is given in your exercise).You test these as (in
Ex1012
's,main
method):