我有这些代码行:
public String getAccountType(BankAccount a){
if(a instanceof RegularAccount)
return "RA";
else if(a instanceof SavingsAccount)
return "SA";
else if(a instanceof cityAccount)
return "CLA";
else if(a instanceof StateLawAccount)
return "SLA";
else if(a instanceof FederationLawAccount)
return "FLA";
else
return null;
}
Run Code Online (Sandbox Code Playgroud)
BankAccount是下面所有类的超类(抽象).在这个方法中,我只想返回String中的"a"类.
但是,我想知道是否有更好的方法来验证除这一堆if/else语句之外的"a"类.有吗?我能用switch语句吗?如果是这样,怎么样?