Java 从同一类的另一个方法中调用方法
我有很多方法需要实现add方法,但是我不知道如何实现,因为所有方法都在同一个类中-ArrayPhoneDirectory 添加方法:Java 从同一类的另一个方法中调用方法,java,methods,method-call,Java,Methods,Method Call,我有很多方法需要实现add方法,但是我不知道如何实现,因为所有方法都在同一个类中-ArrayPhoneDirectory 添加方法: private void add(String name, String telno) { if (size >= capacity) { reallocate(); } theDirectory[size] = new DirectoryEntry(name, telno); size = size
private void add(String name, String telno) {
if (size >= capacity)
{
reallocate();
}
theDirectory[size] = new DirectoryEntry(name, telno);
size = size +1;
}
以下是需要调用add的方法:
负载:
addChangeEntry:
public String addChangeEntry(String name, String telno) {
for (DirectoryEntry x : theDirectory) {
if (x.getName().equals(name)) {
x.setNumber(telno);
return x.getNumber();
} else {
// add a new entry to theDirectory using method add
}
}
return null;
}
这可能是非常明显的,但是我对java还是相当陌生的,所以对于如何调用这些方法的任何帮助都将不胜感激 您只需调用
add(name,telno)
即可
实际上,您的add
方法是私有的,因此只能从类内部调用它
例如,在加载方法中:
public void loadData(String sourceName) {
Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");
while (scan.hasNextLine()) {
String name = scan.nextLine();
String telno = scan.nextLine();
// You don't need this, add builds its own DirectoryEntry from name and telno
// DirectoryEntry newdir = new DirectoryEntry(name, telno);
add(name, telno);
}
}
你可以在
addChangeEntry
@tod中做同样的事情,我想他是在问如何调用它。
public void loadData(String sourceName) {
Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");
while (scan.hasNextLine()) {
String name = scan.nextLine();
String telno = scan.nextLine();
// You don't need this, add builds its own DirectoryEntry from name and telno
// DirectoryEntry newdir = new DirectoryEntry(name, telno);
add(name, telno);
}
}