public Object getValue()
{
ValueItem valueItem = null;
Object returnValue = null;
if(this.value instanceof StringValueImpl)
{
valueItem = (StringValueImpl) this.value;
}
else if(this.value instanceof ListValueImpl)
{
valueItem = (ListValueImpl) this.value;
}
else if(this.value instanceof MapValueImpl)
{
valueItem = (MapValueImpl) this.value;
}
if(valueItem!=null)
returnValue = valueItem.getValue();
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
ValueItem是的interface这是由执行ListValueImpl,MapValueImpl等等.我想返回值是一个object.代码工作正常但我想知道这是否可以以任何方式改进?
是什么类型的this.value?如果是,ValueItem那么你不需要做任何这个,并可以用这个替换方法:
public Object getValue()
{
Object returnValue = null;
if(this.value!=null)
returnValue = this.value.getValue();
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
甚至更短:
public Object getValue()
{
return this.value!=null ? this.value.getValue() : null;
}
Run Code Online (Sandbox Code Playgroud)
如果this.value是没有类型的ValueItem ,但它必须包含一个ValueItem,那么你在你的手有一个设计问题.
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |