我想知道如何能够直接在我的类上而不是在其 ArrayList 变量上调用 contains
方法。最好的解释方法就是使用下面的代码。
感谢您的帮助。
public class Chord {
ArrayList<Note> notes;
// some more stuff not here
}
cmaj = new Chord("Cmaj");
cnote = new Note("C");
// what I have
cmaj.getNotes().contains(cnote);
// what I would like, is this possible, how?
cmaj.contains(cnote);
最佳答案
那么你可以自己编写方法:
public class Chord {
private final List<Note> notes;
...
public boolean contains(Note note) {
return notes.contains(note);
}
}
关于java - 如何直接在类而不是集合上调用 contains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981841/