问题描述
- 改写代码:请问下面的代码 怎么用循环或者遍历的方式改写呀
-
public TodayExamWidget() { self = binder.createAndBindUi(this); int row =1 ; gridWidget = new Grid(); gridWidget.resize(row,3); gridWidget.setSize("100","100"); gridWidget.setText(0,0,"Uncorrected"); gridWidget.addStyleName("gray_cell"); gridWidget.setBorderWidth(1); gridWidget.getCellFormatter().setWidth(0,0,"160"); gridWidget.getCellFormatter().setWidth(0,1,"60"); gridWidget.getCellFormatter().setWidth(0,2,"60"); visionFlowPanel.add(gridWidget); } private void populateVisualAcuity(Grid gridWidget, VisualAcuityCollection visualAcuityCollection) { int rowCount = gridWidget.getRowCount(); if(!visualAcuityCollection.normal.isEmpty()){ gridWidget.resize(rowCount +1,3); gridWidget.setText(rowCount -2,0,VisualAcuityType.NORMAL.toString()); gridWidget.setText(rowCount -2,1,visualAcuityCollection.normal.od.distance); gridWidget.setText(rowCount -2,2,visualAcuityCollection.normal.os.distance); } if(!visualAcuityCollection.pinhole.isEmpty()){ gridWidget.resize(rowCount +1,3); gridWidget.setText(rowCount -2,0,VisualAcuityType.PINHOLE.toString()); gridWidget.setText(rowCount -2,1,visualAcuityCollection.pinhole.od.distance); gridWidget.setText(rowCount -2,2,visualAcuityCollection.pinhole.os.distance); } if(!visualAcuityCollection.binocular.isEmpty()){ gridWidget.resize(rowCount +1,3); gridWidget.setText(rowCount -2,0,VisualAcuityType.BINOCULAR.toString()); gridWidget.setText(rowCount -2,1,visualAcuityCollection.binocular.od.distance); gridWidget.setText(rowCount -2,2,visualAcuityCollection.binocular.os.distance); } row = rowCount; } 主要是上面的那个popluteVisualVison 方法, 又没有什么方法让代码 用loop形式改写? 谢谢啦 各位大神门
解决方案
Java下的Lambda好丑陋。
解决方案二:
能否循环,则要看 visualAcuityCollection.normal、visualAcuityCollection.pinhole 和 visualAcuityCollection.binocular 是否是相同的类型?
如果是,则可以考虑用一个变量来代替它们实现循环。循环内的代码也不一样,看看有没有什么规律吧,如: VisualAcuityType。
解决方案三:
用lambda表达式可以做到
Function [] funcs = new Function<>[]{visualAcuityCollection.normal::isEmpty, visualAcuityCollection.pinhole::isEmpty, visualAcuityCollection.binacular::isEmpty};
for(Function func : funs) {
....
}
时间: 2024-12-30 20:07:21