我被分配了一个任务来创建一个包含界面使用的电话簿。我从 DirectoryEntry 开始,它通过 get 和 set 方法定义联系人姓名和号码。然后为我提供了一个接口(interface),现在必须实现它。
但是我一直不知道如何实现第一个方法,即 loadData:
void loadData(String sourceName);
/** Look up an entry.
@param name The name of person to look up
@return The telno or null if name is not in the directory
*/
我已经获得了一个可以遵循的伪代码模板,但是我仍然不确定如何处理它,因此我们将不胜感激!
伪代码 stub :
create a Scanner to read file
while (not end of file)
use Scanner to read the name
use Scanner to read the telno
create a DirectoryEntry
add the new entry to theDirectory
close file
这是我迄今为止最接近的一次,但我仍然认为我过得很好:
public loadData (String sourceName)
{
// Creating new scanner object
Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");
while (scan.hasNextLine())
{
scan.findInLine(sourceName);
}
预先感谢您的帮助。
最佳答案
方法签名应为 void:
public void loadData(String sourceName)
但是,使用名为“loadData”的 void 方法来查找数据似乎很奇怪,因为它不返回任何内容。您确定那里的注释“查找条目”与此方法一致吗?我希望这样的方法签名与从一个地方获取数据并将其放在另一个地方的方法相对应,可能是类中的私有(private)字段(如某种 Map)。
关于java - 实现接口(interface)和扫描仪属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485199/