有人可以告诉我是否可以通过执行以下操作将 2 个类变为 1 个来减少我的类中的方法数量:
public void duplicateEntries(String personName, String entryType) throws CustomException
{
for (Entry entry : allEntries)
{
if ( entry instanceof entryType)
{
if (personName.equalsIgnoreCase(entry.getName()))
{
throw new CustomException("\nAn entry for " +
personName + "already exists. Entry has been cancelled.");
}
}
}
}
不编译,编译器在线报告“找不到符号 - entryType”:
if ( entry instanceof entryType)
原始代码:
public void duplicatePersonal(String personName) throws CustomException
{
for (Entry entry : allEntries)
{
if ( entry instanceof Personal)
{
if (personName.equalsIgnoreCase(entry.getName()))
{
throw new CustomException("\nAn entry for " +
personName + "already exists. Entry has been cancelled.");
}
}
}
}
public void duplicateBusiness(String personName) throws CustomException
{
for (Entry entry : allEntries)
{
if ( entry instanceof Business)
{
if (personName.equalsIgnoreCase(entry.getName()))
{
throw new CustomException("\nAn entry for " +
personName + "already exists. Entry has been cancelled.");
}
}
}
}
我知道它并没有将我的代码最小化,但是有一些类似的方法我也可以应用它。