实验八 内部类,匿名类
实验内容:(一)内部类、匿名类(必做)
1、三大体育媒体互联网平台,由于资源独占,有些类别的运动只能在某个平台上进行独播;
2、足球赛事只能在平台A上进行独播,篮球赛事只能在平台B上进行独播,排球赛事只能在平台C上进行独播,其他赛事可以在几个平台上进行直播;
3、要求:
(1)三个体育直播平台EA、EB、EC设计为三个类
(2)设计一个体育赛事的类 SportsGame
(3)设计足球赛事,篮球赛事,排球赛事为体育赛事的三个子类,并分别作为三个平台的内部类
(4)其他羽毛球、乒乓球等赛事设计为普通的类,继承自体育赛事
(5)可以根据不同赛事的特点,为其设计内部的成员变量和成员函数
(6)利用匿名类新增其他的体育赛事
(7)在main函数中进行测试
(8)增加比赛规则接口 IRule,令所有的体育赛事实现该接口,重新制定各自的比赛规则
(二)图形化界面设计
1、实现如下图所示的布局
2、产生N个随机整数,并可以对随机数进行升序和降序 *** 作(选做)
// lsy package java_experiments; public class eight_one { public static void main(String[] args) { EA ea = new EA(); EA.Football football = ea.new Football(22, "李铁"); football.game(); // 匿名类 new SportsGame(8) { @Override public void rule() { System.out.println("比赛规则为用时最短者胜出!n"); } public void game() { System.out.println(getSportsman() + "人参加短跑运动"); rule(); } }.game(); } } abstract class SportsGame implements IRule { int sportsman; public SportsGame() { } public SportsGame(int sportsman) { this.sportsman = sportsman; } public int getSportsman() { return sportsman; } public abstract void game(); } class EA { EA() { System.out.println("欢迎观看A平台直播..."); } class Football extends SportsGame { String coach; Football(int sportsman, String coach) { super(sportsman); this.coach = coach; } public void game() { System.out.println(getSportsman() + "人参加足球赛事"); rule(); strategy(); } public void setCoach(String coach) { this.coach = coach; } public void strategy() { System.out.println("教练" + coach + "正在商量策略n"); } @Override public void rule() { System.out.println("比赛规则为进球多的队伍获胜!"); } } class EB { EB() { System.out.println("欢迎观看B平台直播..."); } class Basketball extends SportsGame { Basketball(int sportsman) { super(sportsman); } public void game() { System.out.println(getSportsman() + "人参加篮球赛事"); rule(); } @Override public void rule() { System.out.println("比赛规则为得分高的选手胜出!n"); } } } class EC { EC() { System.out.println("欢迎观看C平台直播..."); } class baseball extends SportsGame { baseball(int sportsman) { super(sportsman); } public void game() { System.out.println(getSportsman() + "人参加排球赛事"); rule(); } @Override public void rule() { System.out.println("比赛规则为得分高的队伍胜出!n"); } } } } class Badminton extends SportsGame { Badminton(int sportsman) { super(sportsman); } public void game() { System.out.println(getSportsman() + "人参加羽毛球赛事"); rule(); } @Override public void rule() { System.out.println("比赛规则为得分高的选手胜出!n"); } } class Pingpong extends SportsGame { Pingpong(int sportsman) { super(sportsman); } public void game() { System.out.println(getSportsman() + "人参加乒乓球赛事"); rule(); } @Override public void rule() { System.out.println("比赛规则为分数高的选手胜出!n"); } } interface IRule { void rule(); }实验结果 内容二代码:
// lsy package java_experiments; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.Jframe; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class eight_two extends Jframe { public eight_two() { ArrayList实验结果jtfs = new ArrayList<>();// 用于保存文本框 Container c = this.getContentPane(); JPanel jp = new JPanel(); int row = 9;// 行数 int col = 2;// 列数 int[] array = new int[8]; jp.setLayout(new GridLayout(row, col, 5, 5)); JLabel jtf1 = new JLabel("排序前数值序列"); JLabel jtf2 = new JLabel("排序后数值序列"); jp.add(jtf1); jp.add(jtf2); for (int i = 0; i < 16; i++) { JTextField jtf = new JTextField(""); jtf.setEditable(false);// 不允许编辑表格 jtfs.add(jtf); jp.add(jtf); } c.add(jp); JPanel jp1 = new JPanel(); final JButton jb = new JButton("随机生成"); final JButton jbOut = new JButton("排序"); Dimension preferredSize = new Dimension(150, 30); // 设置尺寸 jb.setPreferredSize(preferredSize); jbOut.setPreferredSize(preferredSize); jp1.add(jb); jp1.add(jbOut); c.add(jp1, BorderLayout.SOUTH); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Random random = new Random();// 创建随机数对象 for (int i = 0; i < array.length; i++) {// 初始化数组元素 array[i] = random.nextInt(50);// 生成50以内的随机数 } int i = 0;// 将i重置为0 for (int a = 0; a < jtfs.size(); a++) { if (a % 2 == 0) { jtfs.get(a).setText(array[i] + ""); i++; } } } }); jbOut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int temp; // 根据角标进行比较, for (int i = 0; i < array.length; i++) { // j是数组的最后一个角标 for (int j = array.length - 1; j > i; j--) { if (array[j] < array[j - 1]) { // 从后往前进行比较,小数往前,一轮之后最小数就在最前面了 temp = array[j - 1]; array[j - 1] = array[j]; array[j] = temp; } } } int i = 0;// 将i重置为0 for (int a = 0; a < jtfs.size(); a++) { if (a % 2 != 0) { jtfs.get(a).setText(array[i] + ""); i++; } } } }); this.setBounds(160, 250, 330, 350); this.setTitle("主窗体"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new eight_two(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)