【问题标题】:How to implement an interface如何实现一个接口
【发布时间】:2014-11-18 23:30:54
【问题描述】:

我有一个填充界面,它将获取当前人口、设置人口和增加人口。我有一个将我的行星设置在数组列表中的主节点,所以现在我需要做的是实现代码,该代码将帮助我根据该行星方法增加该行星的人口。这是人口界面。

/**
 * This interface models the behavior of planets when travelers arrive 
 * and when they try to leave.
 * @author 
 *
 */
public interface Populate {

/**
 * Get the current population of the planet
 * @return the current population of the planet
 * @throws Exception if the value of the currentPopulation is none of your business
 */
public int getCurrentPopulation() throws Exception;
/**
 * Initialize the current population to a value
 * @param currentPopulation The value to initialize to
 * @return The new value of the current population, will be currentPopulation
 * @throws Exception if the value of the currentPopulation argument is negative
 */
public int setCurrentPopulation(int currentPopulation) throws Exception;
/***
 * Increase the population of the planet as travelers arrive.
 * @param populationIncrease The amount to increase the current population. Can be negative.
 * @return The new current population
 */
public int IncreasePopulation(int populationIncrease);

}

这里是主要的:

public class Main {

public static void main(String[] args) {
    //this class will run calculations for planets

    ArrayList<WorldOfAdams> myPlanets = new ArrayList<WorldOfAdams>(); 
    myPlanets.add(new AllosimaniusSyneca()); 
    myPlanets.add(new BlagulonKappa()); 
    myPlanets.add(new Damogran()); 
    myPlanets.add(new Traal()); 

//Get current population for each of the planets
    for (int i=0; i<myPlanets.size(); i++) {
    myPlanets.get(i).getCurrentPopulation();

每个星球都有自己的一套指令。有 Allosimanius Syneca、Blagulon Kappa、Damogra 和 Traal。

Allosimanius Syneca 的说明

/**
 * This class models the planet Allosimanius Syneca. On this planet travelers are not 
 *welcome. Anyone landing on the planet at any of the 3 spaceports is immediately put 
 *to work in the cinnamon mines. No one is ever allowed to leave.
 */

根据这个星球的方法,当前人口似乎将被设置为 0,因为没有人被允许访问这个星球,但在增加的人口中,我需要反映存在增加,但旅行者将直接前往肉桂矿。我该如何实施?我知道我需要创建将增加人口的代码,然后打印显示人口增加“进入肉桂矿”的声明。我要把这段代码放在main中吗?

Blagulon Kappa 的说明:

 /***
 * All travelers who arrive on this planet are treated with kindness. They may stay as 
 * long as they like and leave whenever they like. However, they will not reveal the 
 *current population to anyone. All inquiries are ignored.

根据这些信息,我需要打印一份声明,说明人口可能会增加,但产出“不关他们的事”。这个星球会不会有一个简单的打印语句并且没有代码来增加人口,因为不允许知道输出?

达莫格兰的说明:

/***
 * Damogran is a mostly peaceful planet that has completely run out of livable land. 
 *They do not allow anyone to visit the planet or leave it. Ever.

所以对于这个星球,我会将当前人口设置为 0,并且人口不会增加。

最后是 Traal 的说明:

/***
* 10% of the travelers arriving on this planet are immediately fed to the Ravenous 
* Bugblatter Beast. Survivors are allowed to come and go as they wish.

对于这个星球,我需要增加人口,但除以 10% 并打印一份声明,其中包含“喂给 Bugblatter Beast”的剩余数字

刚刚学习如何使用接口和抽象类,很难理解如何实现所有这些类以一起运行并能够给出不同的结果。非常感谢您提供的任何帮助或指导!

【问题讨论】:

  • 您在什么变量中存储人口?为什么不增加那个变量呢?
  • 检查你的超类WorldOfAdams,它可能有一个变量来存储currentPopulation。你会想要增加那个变量,可能是currentPopulation += populationIncrease
  • 增加值的能力意味着您正在存储所述值。添加一个类成员来存储人口。将增加的数量添加到存储的人口中。
  • 正如 Miche 所说,您确实没有提供足够的信息来充分回答您。
  • @NewbieGirl Miche 要求你提供作业全文的原因是这里没有足够的信息可以提供帮助。有一小段代码没有上下文,没有超类来帮助我们确定人口是如何存储的,等等。不要让人们猜测,如果我们无法读懂你的想法或弄清楚你的想法,也不要吝啬家庭作业应该是。

标签: java arrays methods interface abstract-class


【解决方案1】:

由于我不想显得粗鲁,我会猜测一下,并建议您的增加人口函数应该如下所示:

@Override
public int IncreasePopulation(int populationIncrease) {

    CinnamonMines CM = getCinnamonMines(); //must be defined somewhere in your code
    CM.IncreasePopulation(populationIncrease);
    return 0;
} 

【讨论】:

  • 我会试试这个。对不起,我的粗鲁作为回报,似乎大多数人每当他们提出关于家庭作业问题的问题时都很粗鲁。我真的很感谢你的帮助!
  • 我其实明白你的意思,对 SO 有一种判断感,可能会令人沮丧且容易陷入。所以你理解这个函数的想法是,不是增加人口,因为人们立即离开去肉桂矿,你直接调用矿井的增加人口方法。让我知道你是否可以让你的锻炼发挥作用。
  • 更新了我的问题以反映更多信息。这个任务让我头晕目眩!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多