如何实现接口

时间:2014-09-24 12:54:15

标签: java arrays methods interface abstract-class

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

/**
 * 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,Damogran和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,因为没有人被允许访问这个星球,但是在增加的人口中,我需要反映出有增加但是旅行者将直奔肉桂矿。我该如何实现呢?我知道我需要创建能够增加人口数量的代码,然后打印声明,表明人口的增加会“到肉桂地雷”。我把这段代码放在主要的吗?

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的说明:

/***
 * 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野兽”

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

1 个答案:

答案 0 :(得分:0)

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

@Override
public int IncreasePopulation(int populationIncrease) {

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