一个非常有用的关于集合的例子,实现集合的add,remove和this[index]方法

Animals.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Animals : DictionaryBase
   {
      public void Add(string newID, Animal newAnimal)
      {
         Dictionary.Add(newID, newAnimal);
      }

      public void Remove(string animalID)
      {
         Dictionary.Remove(animalID);
      }

      public new IEnumerator GetEnumerator()
      {
         foreach (object animal in Dictionary.Values)
            yield return (Animal)animal;
      }

      public Animal this[string animalID]
      {
         get
         {
            return (Animal)Dictionary[animalID];
         }
         set
         {
            Dictionary[animalID] = value;
         }
      }
   }

}
View Code
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   class Program
   {
      static void Main(string[] args)
      {
         Animals animalCollection = new Animals();
         animalCollection.Add("Jack", new Cow("Jack"));
         animalCollection.Add("Vera", new Chicken("Vera"));
         foreach (Animal myAnimal in animalCollection)
         {
            Console.WriteLine("New {0} object added to custom collection, " +
                              "Name = {1}", myAnimal.ToString(), myAnimal.Name);
         }
         Console.ReadKey();
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public abstract class Animal
   {
      protected string name;

      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      public Animal()
      {
         name = "The animal with no name";
      }

      public Animal(string newName)
      {
         name = newName;
      }

      public void Feed()
      {
         Console.WriteLine("{0} has been fed.", name);
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Chicken : Animal
   {
      public void LayEgg()
      {
         Console.WriteLine("{0} has laid an egg.", name);
      }

      public Chicken(string newName) : base(newName)
      {
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Cow : Animal
   {
      public void Milk()
      {
         Console.WriteLine("{0} has been milked.", name);
      }

      public Cow(string newName) : base(newName)
      {
      }
   }
}
View Code

 

 

转载于:https://www.cnblogs.com/swtool/p/5524832.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 JList 和 ListSelectionListener 来实现根据集合给一份列表框,并添加单选按钮、编辑按钮、增加按钮和删除按钮。以下是一个示例代码: ```java import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; public class MyList extends JPanel implements ListSelectionListener { private JList<String> list; private ArrayList<String> data; private JRadioButton activeButton; private JButton editButton; private JButton addButton; private JButton deleteButton; public MyList() { super(new BorderLayout()); data = new ArrayList<String>(); data.add("Item 1"); data.add("Item 2"); data.add("Item 3"); data.add("Item 4"); list = new JList<String>(data.toArray(new String[0])); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0); list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); activeButton = new JRadioButton("Active"); activeButton.setSelected(true); editButton = new JButton("Edit"); editButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); if (index >= 0) { String item = data.get(index); String newItem = JOptionPane.showInputDialog(null, "Edit Item", item); if (newItem != null) { data.set(index, newItem); list.setListData(data.toArray(new String[0])); list.setSelectedIndex(index); } } } }); addButton = new JButton("Add"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String newItem = JOptionPane.showInputDialog(null, "Add Item"); if (newItem != null) { data.add(newItem); list.setListData(data.toArray(new String[0])); list.setSelectedIndex(data.size() - 1); } } }); deleteButton = new JButton("Delete"); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); if (index >= 0) { data.remove(index); list.setListData(data.toArray(new String[0])); int size = data.size(); if (size == 0) { editButton.setEnabled(false); deleteButton.setEnabled(false); } else { if (index == size) { index--; } list.setSelectedIndex(index); } } } }); JPanel buttonPanel = new JPanel(new GridLayout(0, 1)); buttonPanel.add(activeButton); buttonPanel.add(editButton); buttonPanel.add(addButton); buttonPanel.add(deleteButton); add(listScrollPane, BorderLayout.CENTER); add(buttonPanel, BorderLayout.EAST); } public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { editButton.setEnabled(false); deleteButton.setEnabled(false); } else { editButton.setEnabled(true); deleteButton.setEnabled(true); } } } private static void createAndShowGUI() { JFrame frame = new JFrame("MyList"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new MyList(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } ``` 在这个例子中,我们创建了一个名为 MyList 的 JPanel,其中包含了一个 JList 和一些按钮。我们使用 ArrayList 来存储列表的数据,并使用 JList 来显示这些数据。根据需求,我们可以使用 addremove 和 set 方法来添加、删除和编辑列表项。 在 JList 上添加 ListSelectionListener,以便在用户选择不同的列表项时更新按钮的状态。我们还为每个按钮添加了 ActionListener,以便在用户点击时执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值