我有一个类似于下面的方法,我只想返回一个类的私有变量,只有在某些条件被验证时才会返回.否则我想退出方法而不返回任何内容.我尝试过类似下面的代码,但我担心返回null并不是一个好主意.有没有办法退出像break关键字一样的方法适用于循环?
private Classxpto classxpto;
public Classxpto getclassxpto(String abc, Date asd){
String curr_abc= classxpto.getabc();
Date curr_asd= classxpto.getasd();
if("my conditions"){
//dont return classxpto
return null;
}else if("my other conditions"){
classxpto.setabc(abc);
classxpto.setasd(asd);
return classxpto;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)