English 中文(简体)
我需要帮助理解抽象课程之间的关系。
原标题:I need help understanding the relationship between abstract classes

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.

这就是我开始建立这一机制的方式。 我想把我的方法和参数放在正确的地点,我很想知道,在什么地方我确定我的老师希望我通过的标准。

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??
        }       
    }   
}

我只是担心这些班级与参数本身之间的关系,因为一旦我有诸如这些班子的多个班子,我就会感到困惑。 任何见解都会受到高度赞赏。 感谢!

最佳回答

摘要方法可能没有机构,因此,您对<代码>的界定应当:

abstract void computeDiscount(int count, float itemCost);

在扩大抽象类别的具体类别中,你必须采用这种方法。 一般来说,抽象的方法与在接口中确定的方法类似(这些方法被宣布,但没有违约执行),但仍然存在差异(可以保护,也可以是私人的一揽子办法,只能由子类实施)。

在大多数情况下,你有一个抽象的班子,提供一些缺省逻辑,并且只是要求分门填写一些取决于具体执行的“空缺”。

基本上,你将参数储存到<代码>。 缩略语 你们再说一遍,这很可能是错误的。 我猜测,你的主人应当把这种方法指直接制造的物体,例如:

DiscountPolicy bngo = new BuyNItemsGetOneFree(5); 
double discountForFour = bngo.computDiscount(4,4.95f);
double discountForFive = bngo.computDiscount(5,4.95f);

请注意,根据您的任务,computeDiscount(...) 方法应退还价值:

......单一抽象的方法 return a. 购买某一项物品的折扣......

Edit:

如何确定计数和项目?

正如我前面说过的那样,你不给他们“set”,而是只用他们计算。

问题回答

你们需要审查如何界定抽象方法。 抽象的方法没有“{}”。 应在次类中加以界定。 当基类使用抽象方法时,子类通常计算折扣。

首先,抽象的方法没有机构。

第二,它应当计算出的折扣,因此它应当回来,以资补偿。 在您的案例中,打折扣就是无效的。

政策

public abstract class DiscountPolicy {  
  abstract float computeDiscount(int count, float itemCost);
}

此外,你不能使用

DiscountPolicy.computeDiscount(int count, float itemCost);

页: 1 贴现方法不是固定方法,甚至不具体。

仅凭一句话,java就是一种敏感的语言,因此,你应当像你所宣称的那样,使用 st子。 说明和赔偿 差异是两个不同的问题。

<代码>DiscountPolicy是一个基础抽象的类别。 就你而言,这为一种代表某种类型差异的类别提供了结构:所有这类类别都应有计算差异的方法,这种方法将根据各自政策计算折扣。 这是代表会计的所有阶层必须遵循的合同。

本身并不给出计算折扣的任何逻辑(不采用“违约政策”)。 每个阶层都必须提供自己的逻辑。 请通过制定<条码>对应代码方法<条码>予以执行。

java的抽象方法没有任何机构。 这只是签字:只是合同(结构)和没有执行。

So the computeDiscount in your DiscountPolicy class should look like (Note the ; at the end of the signature itself. Also no {}) :

abstract float computeDiscount(int count, float itemCost);

此外,这一方法将退还购买某一项物品的折扣,因此,返还类型应为<条码>float ,而不是<条码>。

<代码>BuyNItemsGet OneFree和BulkDiscount的班次,为<代码>DiscountPolicy 执行<编码> 比较简明分辨率/代码>。 两种次类的“彩虹/代码”方法的逻辑将有所不同,其依据是批量折扣的贴现计算逻辑和购买N免费(在你的工作中给出这一逻辑)。

class BulkDiscount extends DiscountPolicy
{
   //Same signature as computeDiscount of DiscountPolicy
   //Not abstract, no semicolon at end of signature, has body. 
   //Also called "Concrete" method
   float computeDiscount(int count, float itemCost)
   {
       //The logic as given in the exercise.
       //Return the discount calculated by the logic
   }
}

请将其作为(Ex1012 s,main方法>)加以测试:

   DiscountPolicy bulk = new BulkDiscount();
   float discount = bulk.computeDiscount(10, 1);  //Data used to test

   DiscountPolicy bngo = new BuyNItemsGetOneFree();  
   float  discount = bngo.computeDiscount()




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...