gpt4 book ai didi

java - 在 Java 中使用类的接口(interface)

转载 作者:行者123 更新时间:2023-12-01 17:00:42 24 4
gpt4 key购买 nike

我对在 Java 中使用接口(interface)有点困惑。我已经为 Bag java 类创建了一个接口(interface)。看来我实现该接口(interface)的唯一方法是在 Bag 类 header 中显式声明该类是抽象的。如果我这样做,那么我将无法创建另一个类,比如说一个使用 bag 类的 PiggyBank java 类。所以这让我对应该如何去做感到有些困惑。是否可以定义一个扩展 Bag 类的 PiggyBank 类?我还没有完成当前的 Bag 类,因为我不确定如何定义某些方法。然后我意识到,由于该类现在是抽象的,我将无法从测试类创建该类的新实例。

package bagimplementation;
/**
An interface that the describes the operations of a bag of objects.
@author Jeff nicholas
* @param <T> The type of item being bagged
*/
public interface BagInterface <T> {
/**Gets the current number of entries in this bag.
@return the integer number of entries currently in the bag*/
int getCurrentSize();

/**Sees whether this bag is full.
@return true if the bag is full, or false if not */
boolean isFull();

/**Sees whether this bag is empty.
@return true if the bag is empty, or false if not */
boolean isEmpty();

/**Adds a new entry to this bag.
@param newEntry the object to be added as a new entry.
@return true if the addition is successful, or false if not */
boolean add(T newEntry);

/**Removes one unspecified entry from this bag, if possible.
@return either the removed entry, if the removal was successful, or null*/
T remove();

/**Removes one occurrence of a given entry from this bag, if possible
@param anEntry the entry the be removed.
@return true if the removal was successful, or false if not */
boolean remove(T anEntry);

/**Removes all entries from this bag. */
void clear();

/**Counts the number of times a given entry appears in this bag.
@param anEntry the entry to be counted.
@return the number of times anEntry appears in the bag */
int getFrequencyOf(T anEntry);

/**Tests whether this bag contains a given entry.
* @param anEntry the entry to locate
* @return true if the bag contains anEntry, or false otherwise */
boolean contaions(T anEntry);

/**Creates an array of all entries that are in this bag.
* @return a newly allocated array of all the entries in the bag*/
T[] toArray();
}//end BagInterface

/**Bag class is not finished at this time */
package bagimplementation;
import java.util.*;
/**
*
* @author admin
*/
public abstract class Bag implements BagInterface<Bag>{

private int size;
private boolean full;
private boolean empty;
private int currentSize;

public void setSize(int aSize){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a size for the bag.....");
aSize = keyboard.nextInt();
size = aSize;
}//end setSize


public int getCurrentSize(){
return currentSize;
}


}

最佳答案

除非您将该类声明为抽象类,否则您必须实现该接口(interface)中的所有方法。如果您不知道如何实现某些方法,只需添加一些像这样的临时 stub 实现,然后当您弄清楚时,将其替换为真正的实现:

public boolean isEmpty() {
// TODO: implement functionality
return false;
}

当您选择实现接口(interface)时,某些 IDE 还会为您生成这些 stub 方法。

关于java - 在 Java 中使用类的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27823441/

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