讓我們看看我的程序。這與汽車模型有一些虛構的.txt文件。作爲函數參數的Java布爾方程
public class Car {
private String model;
public String getModel() {
return this.model;
}
}
public class Main {
public static void Main(String[] args) {
ArrayList<Car> cars = loadCars(path); //imaginary func and file
String someModel = generateSomeModel(); //imaginart func
for (Car c : cars)
if (c.getModel.equals(someModel))
System.out.println("Hit!");
for (Car c : cars)
if (!c.getModel.equals(someModel))
System.out.println("Hit!");
}
}
讓我們假設,我要通過我的汽車列出100次,他們在比較,如果爲每一段不同的屬性聲明,這意味着我將不得不寫100個不同的for循環,這將是90%相同。有沒有辦法編寫一個函數作爲參數布爾方程?像這樣的東西。
public static void printCars(Boolean equation) {
for (Car c : cars)
if (Boolean equation)
System.out.println("Hit!");
}
我現在認識到你沒有把你的問題標記爲java-8。你在使用java 8還是使用以前的版本? –