这是一个最小的工作示例:
public class FF {
@Test
public void test01() {
final ListProperty p = new SimpleListProperty(FXCollections.observableArrayList());
p.addListener((ListChangeListener) c -> {
System.err.println("Listener here..");
});
Bindings.createObjectBinding(() -> {
System.err.println("Binding here");
return null;
}, p);
p.add("hans");
}
@Test
public void test02() {
final ListProperty p = new SimpleListProperty(FXCollections.observableArrayList());
final ListProperty p2 = new SimpleListProperty(FXCollections.observableArrayList());
p.addListener((ListChangeListener) c -> {
System.err.println("Listener 1 here..");
});
p2.addListener((ListChangeListener) c -> {
System.err.println("Listener 2 here..");
});
final ObjectBinding ob = Bindings.createObjectBinding(() -> {
System.err.println("Binding here");
return null;
}, p);
p2.bind(ob);
p.add("hans");
}
}
第二次测试看起来像预期,但对于第一次测试,输出只是“Listener here ..”。为什么绑定在这种情况下不起作用?
匿名侦听器和匿名绑定有什么区别?
答案 0 :(得分:2)
你已经创建了Binding对象但没有通过它绑定任何东西,因此它优化了自己什么都不做。
添加任意随机操作以查看“此处绑定...”输出。 E.g:
###########################################################
# Algorithm
# Ask for a word
# Count and store all vowels in the words (repeats only are
# counted once)
# count and store only consonants after the last vowel, if the
# if last letter is a vowel no consonants are stored
# once all 5 vowels are stored or at least 5 consonants are
# stored
# print
# what vowels appear and how many
# what consonants appear
###########################################################
VOWELS = 'aeiou'
word = input('Input a word: ')
wordlow = word.lower() #converts the input to all lowercase
vowcount = 0
concount = 0
vowcollected_str = ''
concollected_str = ''
#ends the program once 5 vowels or consonants have been stored
while vowcount <= 4 and concount <= 4:
vowcount = 0
concount = 0
#stores the actual letter that is being stored, not just how many
vowcollected_str = ''
concollected_str = ''
for i, ch in enumerate(wordlow):
if ch in VOWELS:
if ch not in vowcollected_str:
vowcollected_str += ch
position = i
vowcount += len(vowcollected_str)
if ch not in VOWELS:
if ch not in concollected_str:
concollected_str += wordlow[i:]
concount += len(word[i:])
word = input('Input a word: ')
wordlow = word.lower()
print(vowcount)
print(vowcollected_str)
print(concount)
print(concollected_str)