Easier way to call same method for multiple variables

前端 未结 1 1961
太阳男子
太阳男子 2021-01-21 19:49

Is it possible to not have to re type all of these almost identical procedures?

cancelSaveContactBtn.setVisible(false);
saveContactBtn.setVisible(false);
addCont         


        
相关标签:
1条回答
  • 2021-01-21 20:32

    Technically if you intend to call the same method with the same parameter on a set of objects you could use a List to store your objects, iterate through and set an addActionListener to each one.

    List<Object> objects = new ArrayList<Object>();
    
    objects.add(selectContactCBox);
    objects.add(addContactBtn);
    objects.add(personalRadio);
    objects.add(businessRadio);
    
    for(object o: objects){
    o.addActionListener(this);
    }
    
    0 讨论(0)
提交回复
热议问题