更容易为多个变量调用相同的方法

时间:2015-10-24 06:24:25

标签: java

是否可以不必重新输入所有这些几乎相同的程序?

cancelSaveContactBtn.setVisible(false);
saveContactBtn.setVisible(false);
addContactBtn.setVisible(true);

selectContactCBox.addActionListener(this);
addContactBtn.addActionListener(this);
personalRadio.addActionListener(this);
businessRadio.addActionListener(this);

1 个答案:

答案 0 :(得分:3)

从技术上讲,如果您打算在一组对象上使用相同的参数调用相同的方法,则可以使用List来存储对象,迭代并为每个对象设置abstract class CharacterClass { private String characterClass; private int level; private int hp; private int strength; private int constitution; private int dexterity; private int intelligence; private int wisdom; private int charisma; protected CharacterClass(){ setCharacterClass("Character Class"); setLevel(1); setHp(10); setStrength(10); setConstitution(10); setDexterity(10); setIntelligence(10); setWisdom(10); setCharisma(10); } public String getCharacterClass() { return characterClass; } public int getLevel() { return level; } public int getHp() { return hp; } public int getStrength(){ return strength; } public int getConstitution(){ return constitution; } public int getDexterity(){ return dexterity; } public int getIntelligence(){ return intelligence; } public int getWisdom(){ return wisdom; } public int getCharisma(){ return charisma; } protected void setCharacterClass(String characterClass){ this.characterClass = characterClass; } protected void setLevel(int lvl){ level = lvl; } protected void setHp(int hitPoints){ hp = hitPoints; } protected void setStrength(int str){ strength = str; } protected void setConstitution(int con){ constitution = con; } protected void setDexterity( int dex) { dexterity = dex; } protected void setIntelligence(int intel){ intelligence = intel; } protected void setWisdom(int wis){ wisdom = wis; } protected void setCharisma(int cha){charisma = cha; } } class Fighter extends CharacterClass { Fighter(){ setCharacterClass("Fighter"); setLevel(1); setHp(10); setStrength(14); setConstitution(16); setDexterity(14); setIntelligence(10); setWisdom(10); setCharisma(10); } }

addActionListener