有没有办法缩短由于对象的相似属性而重复的代码?

Is there a way to shorten code that is duplicated because of similiar properties of objects?

我有很多复选框,所有这些都依赖于某个未被选中的框,但是如果它被选中,则必须选中并隐藏这些框。我知道人们一直在说 ArrayList,但是我不知道如何编辑 ArrayList 中项目的某些属性的语法。

我的代码有效,我只需要缩短此代码,因为我相信如果它继续 运行 这样,它最终会减慢进程,我想了解它是如何工作的我拥有的其他对象也是如此。

 public void cbxSalesSelectA() {
    boolean t = cbx_SALES_Select_All.getText().equals("Select All");
    cbx_SALESQtySold.setSelected(t);
    cbx_SALESDateSold.setSelected(t);
    cbx_SALESCustomer.setSelected(t);
    cbx_SALESDiscount.setSelected(t);
    cbx_SALESLineNumber.setSelected(t);
    cbx_SALESConsultant.setSelected(t);
    cbx_SALES_Header_Row.setSelected(t);
    if (t) {
        cbx_SALES_Select_All.setText("Deselect All");
    } else {
        cbx_SALES_Select_All.setText("Select All");
    }
}
 public void cbxLOCSelectA() {
    boolean t = cbx_LOC_Select_All.getText().equals("Select All");
    cbx_LOCHeight.setSelected(t);
    cbx_LOCWidth.setSelected(t);
    cbx_LOCDepth.setSelected(t);
    cbx_LOCWeightCap.setSelected(t);
    cbx_LOCAccessibility.setSelected(t);
    cbx_LOC_Header_Row.setSelected(t);
    if (t) {
        cbx_LOC_Select_All.setText("Deselect All");
    } else {
        cbx_LOC_Select_All.setText("Select All");
    }
}

如果您使用 ArrayList 或复选框对象数组,您只需使用循环 运行 遍历所有复选框并根据需要选中或取消选中它们。

例如,

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class Example  extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        // Creating an ArrayList here and adding all the checkboxes required. In your
        // case you'd add your existing checkboxes
        List<CheckBox> list = new ArrayList<>();
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());

        // this combobox controls whether or not all checkboxes are selected
        ComboBox<String> comboBox = new ComboBox<>();

        // two options to either select all or deselect all checkboxes
        comboBox.setItems(FXCollections.observableArrayList("Select All", "Deselect All"));

        // container to hold all the controls
        VBox vBox = new VBox();
        vBox.getChildren().add(comboBox);
        vBox.getChildren().addAll(list);

        // this is the important bit
        // if the combobox selection is changed, then this fires
        comboBox.setOnAction(event -> {
            // if the selected option is select all, then a for-each loop is used to make all the 
            // checkboxes in the arraylist checked and vice versa if the deselect option is selected
            if (comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase("Select All")) {
                for (CheckBox checkBox : list) checkBox.setSelected(true);
            } else {
                for (CheckBox checkBox : list) checkBox.setSelected(false);
            }
        });

        primaryStage.setScene(new Scene(vBox));
        primaryStage.setTitle("Example");
        primaryStage.setWidth(600);
        primaryStage.setHeight(400);
        primaryStage.show();
    }
}

希望对您有所帮助。

我不认为您真的可以提高代码的性能,但为了提高可读性,您可以在单独的方法中对 selected 进行更改。例如

static void allSetSelected(boolean isSelected, CheckBox... boxes ) {
    Arrays.stream(boxes).forEach(b -> b.setSelected(isSelected));
}

并像这样在您的代码中使用它

public void cbxSalesSelectA() {
    boolean t = cbx_SALES_Select_All.getText().equals("Select All");
    allSetSelected(t, cbx_SALESQtySold,
                      cbx_SALESDateSold,
                      cbx_SALESCustomer,
                      cbx_SALESDiscount,
                      cbx_SALESLineNumber,
                      cbx_SALESConsultant,
                      cbx_SALES_Header_Row)
    if (t) {
        cbx_SALES_Select_All.setText("Deselect All");
    } else {
        cbx_SALES_Select_All.setText("Select All");
    }
}

public void cbxLOCSelectA() {
    boolean t = cbx_LOC_Select_All.getText().equals("Select All");
    allSetSelected(t, cbx_LOCHeight, cbx_LOCWidth, cbx_LOCDepth, cbx_LOCWeightCap, cbx_LOCAccessibility, cbx_LOC_Header_Row);
    if (t) {
        cbx_LOC_Select_All.setText("Deselect All");
    } else {
        cbx_LOC_Select_All.setText("Select All");
    }
}