Java 谜题4:液体(Liquid)

19-09-19 jdon

你可以通过机场保安计划吗?

您的手提行李不允许携带液体:

package liquid;
 
import java.lang.String;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
 
public class Luggage {
    private final Collection<String> items;
 
    public Luggage(Collection<String> items) {
        items = Collections.unmodifiableCollection(new ArrayList<String>(items));
        for (String item : items) {
            if (item.contains("liquid")) {
                throw new SecurityException("No liquids allowed in your hand luggage!");
            }
        }
        this.items = items;
    }
 
    public Collection<String> getItems() {
        return items;
    }
 
    public void fly() {
        if (items.contains("liquid water")) {
            // The goal is to reach this line
            System.out.println("Oh no, water on a plane! We're all going to die!");
        }
    }
}

作为一个口渴的乘客,你能随身带些水吗?

    

猜你喜欢