今日内容
- 一、内部类
- 1.内部类的概念与分类
- 2.成员内部类的定义
- 3.成员内部类的使用
- 4.内部类的同名变量访问
- 5.局部内部类定义
- 6.局部内部类的final问题
- 7.匿名内部类
- 匿名内部类定义格式
- 匿名内部类的注意事项
- 8.类作为成员变量类型
- 9.接口作为成员变量类型
- 10.接口作为方法的参数和或返回值
- 11.发红包案例_普通红包平均分发及手气红包随机分发
- 12.JDK9的JShell简单使用
- 13.编译器的两点优化
一、内部类
1.内部类的概念与分类
如果一个事物的内部包含另一个事物,那么这就是一个类内部包含另一个类。
例如:身体和心脏的关系。又如:汽车和发动机的关系。
分类:
- 成员内部类
- 局部内部类(包含匿名内部类)
2.成员内部类的定义
成员内部类的定义格式:
修饰符 class 外部类名称 {
修饰符 class 内部类名称 {
// ...
}
// ...
}
注意:内用外,随意访问;外用内,需要内部类对象。
package cn.itcast.day11.demo03;
public class Body { // 外部类
public class Heart { // 成员内部类
// 内部类的方法
public void beat() {
System.out.println("心脏跳动:蹦蹦蹦!");
System.out.println("我叫:" + name); // 正确写法!
}
}
// 外部类的成员变量
private String name;
// 外部类的方法
public void methodBody() {
System.out.println("外部类的方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.成员内部类的使用
package cn.itcast.day11.demo03;
public class Body { // 外部类
public class Heart { // 成员内部类
// 内部类的方法
public void beat() {
System.out.println("心脏跳动:蹦蹦蹦!");
System.out.println("我叫:" + name); // 正确写法!
}
}
// 外部类的成员变量
private String name;
// 外部类的方法
public void methodBody() {
System.out.println("外部类的方法");
new Heart().beat();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
如何使用成员内部类?有两种方式:
1. 间接方式:在外部类的方法当中,使用内部类;然后main只是调用外部类的方法。
2. 直接方式,公式:
类名称 对象名 = new 类名称();
【外部类名称.内部类名称 对象名 = new 外部类名称().new 内部类名称();】
*/
public class Demo01InnerClass {
public static void main(String[] args) {
Body body = new Body(); // 外部类的对象
// 通过外部类的对象,调用外部类的方法,里面间接在使用内部类Heart
body.methodBody();
System.out.println("=====================");
// 按照公式写:
Body.Heart heart = new Body().new Heart();
heart.beat();
}
}
4.内部类的同名变量访问
package cn.itcast.day11.demo03;
// 如果出现了重名现象,那么格式是:外部类名称.this.外部类成员变量名
public class Outer {
int num = 10; // 外部类的成员变量
public class Inner /*extends Object*/ {
int num = 20; // 内部类的成员变量
public void methodInner() {
int num = 30; // 内部类方法的局部变量
System.out.println(num); // 局部变量,就近原则
System.out.println(this.num); // 内部类的成员变量
System.out.println(Outer.this.num); // 外部类的成员变量
}
}
}
package cn.itcast.day11.demo03;
public class Demo02InnerClass {
public static void main(String[] args) {
// 外部类名称.内部类名称 对象名 = new 外部类名称().new 内部类名称();
Outer.Inner obj = new Outer().new Inner();
obj.methodInner();
}
}
5.局部内部类定义
package cn.itcast.day11.demo04;
/*
如果一个类是定义在一个方法内部的,那么这就是一个局部内部类。
“局部”:只有当前所属的方法才能使用它,出了这个方法外面就不能用了。
定义格式:
修饰符 class 外部类名称 {
修饰符 返回值类型 外部类方法名称(参数列表) {
class 局部内部类名称 {
// ...
}
}
}
小节一下类的权限修饰符:
public > protected > (default) > private
定义一个类的时候,权限修饰符规则:
1. 外部类:public / (default)
2. 成员内部类:public / protected / (default) / private
3. 局部内部类:什么都不能写
*/
class Outer {
public void methodOuter() {
class Inner { // 局部内部类
int num = 10;
public void methodInner() {
System.out.println(num); // 10
}
}
Inner inner = new Inner();
inner.methodInner();
}
}
package cn.itcast.day11.demo04;
public class DemoMain {
public static void main(String[] args) {
Outer obj = new Outer();
obj.methodOuter();
}
}
6.局部内部类的final问题
package cn.itcast.day11.demo04;
/*
局部内部类,如果希望访问所在方法的局部变量,那么这个局部变量必须是【有效final的】。
备注:从Java 8+开始,只要局部变量事实不变,那么final关键字可以省略。
原因:
1. new出来的对象在堆内存当中。
2. 局部变量是跟着方法走的,在栈内存当中。
3. 方法运行结束之后,立刻出栈,局部变量就会立刻消失。
4. 但是new出来的对象会在堆当中持续存在,直到垃圾回收消失。
*/
public class MyOuter {
public void methodOuter() {
int num = 10; // 所在方法的局部变量
class MyInner {
public void methodInner() {
System.out.println(num);
}
}
}
}
7.匿名内部类
匿名内部类定义格式
package cn.itcast.day11.demo05;
public interface MyInterface {
void method1(); // 抽象方法
}
package cn.itcast.day11.demo05;
public class MyInterfaceImpl implements MyInterface {
@Override
public void method1() {
System.out.println("实现类覆盖重写了方法!111");
}
}
package cn.itcast.day11.demo05;
/*
如果接口的实现类(或者是父类的子类)只需要使用唯一的一次,
那么这种情况下就可以省略掉该类的定义,而改为使用【匿名内部类】。
匿名内部类的定义格式:
接口名称 对象名 = new 接口名称() {
// 覆盖重写所有抽象方法
};
*/
public class DemoMain {
public static void main(String[] args) {
// MyInterface obj = new MyInterfaceImpl();
// obj.method();
// MyInterface some = new MyInterface(); // 错误写法!
// 使用匿名内部类
MyInterface obj = new MyInterface() {
@Override
public void method1() {
System.out.println("匿名内部类实现了方法!111-A");
};
obj.method1();
}
匿名内部类的注意事项
package cn.itcast.day11.demo05;
public interface MyInterface {
void method1(); // 抽象方法
void method2();
}
package cn.itcast.day11.demo05;
public class MyInterfaceImpl implements MyInterface {
@Override
public void method1() {
System.out.println("实现类覆盖重写了方法!111");
}
@Override
public void method2() {
System.out.println("实现类覆盖重写了方法!222");
}
}
package cn.itcast.day11.demo05;
/*
如果接口的实现类(或者是父类的子类)只需要使用唯一的一次,
那么这种情况下就可以省略掉该类的定义,而改为使用【匿名内部类】。
匿名内部类的定义格式:
接口名称 对象名 = new 接口名称() {
// 覆盖重写所有抽象方法
};
对格式“new 接口名称() {...}”进行解析:
1. new代表创建对象的动作
2. 接口名称就是匿名内部类需要实现哪个接口
3. {...}这才是匿名内部类的内容
另外还要注意几点问题:
1. 匿名内部类,在【创建对象】的时候,只能使用唯一一次。
如果希望多次创建对象,而且类的内容一样的话,那么就需要使用单独定义的实现类了。
2. 匿名对象,在【调用方法】的时候,只能调用唯一一次。
如果希望同一个对象,调用多次方法,那么必须给对象起个名字。
3. 匿名内部类是省略了【实现类/子类名称】,但是匿名对象是省略了【对象名称】
强调:匿名内部类和匿名对象不是一回事!!!
*/
public class DemoMain {
public static void main(String[] args) {
// MyInterface obj = new MyInterfaceImpl();
// obj.method();
// MyInterface some = new MyInterface(); // 错误写法!
// 使用匿名内部类,但不是匿名对象,对象名称就叫objA
MyInterface objA = new MyInterface() {
@Override
public void method1() {
System.out.println("匿名内部类实现了方法!111-A");
}
@Override
public void method2() {
System.out.println("匿名内部类实现了方法!222-A");
}
};
objA.method1();
objA.method2();
System.out.println("=================");
// 使用了匿名内部类,而且省略了对象名称,也是匿名对象
new MyInterface() {
@Override
public void method1() {
System.out.println("匿名内部类实现了方法!111-B");
}
@Override
public void method2() {
System.out.println("匿名内部类实现了方法!222-B");
}
}.method1();
// 因为匿名对象无法调用第二次方法,所以需要再创建一个匿名内部类的匿名对象
new MyInterface() {
@Override
public void method1() {
System.out.println("匿名内部类实现了方法!111-B");
}
@Override
public void method2() {
System.out.println("匿名内部类实现了方法!222-B");
}
}.method2();
}
}
8.类作为成员变量类型
package cn.itcast.day11.demo06;
// 游戏当中的英雄角色类
public class Hero {
private String name; // 英雄的名字
private int age; // 英雄的年龄
private Weapon weapon; // 英雄的武器
public Hero() {
}
public Hero(String name, int age, Weapon weapon) {
this.name = name;
this.age = age;
this.weapon = weapon;
}
public void attack() {
System.out.println("年龄为" + age + "的" + name + "用" + weapon.getCode() + "攻击敌方。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
}
package cn.itcast.day11.demo06;
public class Weapon {
private String code; // 武器的代号
public Weapon() {
}
public Weapon(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
package cn.itcast.day11.demo06;
public class DemoMain {
public static void main(String[] args) {
// 创建一个英雄角色
Hero hero = new Hero();
// 为英雄起一个名字,并且设置年龄
hero.setName("盖伦");
hero.setAge(20);
// 创建一个武器对象
Weapon weapon = new Weapon("AK-47");
// 为英雄配备武器
hero.setWeapon(weapon);
// 年龄为20的盖伦用多兰剑攻击敌方。
hero.attack();
}
}
9.接口作为成员变量类型
package cn.itcast.day11.demo07;
public class Hero {
private String name; // 英雄的名称
private Skill skill; // 英雄的技能
public Hero() {
}
public Hero(String name, Skill skill) {
this.name = name;
this.skill = skill;
}
public void attack() {
System.out.println("我叫" + name + ",开始施放技能:");
skill.use(); // 调用接口中的抽象方法
System.out.println("施放技能完成。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
}
package cn.itcast.day11.demo07;
public interface Skill {
void use(); // 释放技能的抽象方法
}
package cn.itcast.day11.demo07;
public class SkillImpl implements Skill {
@Override
public void use() {
System.out.println("Biu~biu~biu~");
}
}
package cn.itcast.day11.demo07;
public class DemoGame {
public static void main(String[] args) {
Hero hero = new Hero();
hero.setName("艾希"); // 设置英雄的名称
// 设置英雄技能
// hero.setSkill(new SkillImpl()); // 使用单独定义的实现类
// 还可以改成使用匿名内部类
// Skill skill = new Skill() {
// @Override
// public void use() {
// System.out.println("Pia~pia~pia~");
// }
// };
// hero.setSkill(skill);
// 进一步简化,同时使用匿名内部类和匿名对象
hero.setSkill(new Skill() {
@Override
public void use() {
System.out.println("Biu~Pia~Biu~Pia~");
}
});
hero.attack();
}
}
10.接口作为方法的参数和或返回值
package cn.itcast.day11.demo07;
import java.util.ArrayList;
import java.util.List;
/*
java.util.List正是ArrayList所实现的接口。
*/
public class DemoInterface {
public static void main(String[] args) {
// 左边是接口名称,右边是实现类名称,这就是多态写法
List<String> list = new ArrayList<>();
List<String> result = addNames(list);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
}
public static List<String> addNames(List<String> list) {
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("玛尔扎哈");
list.add("沙扬娜拉");
return list;
}
}
11.发红包案例_普通红包平均分发及手气红包随机分发
package cn.itcast.day11.red;
import java.util.ArrayList;
public interface OpenMode {
/**
* 请将totalMoney分成count份,保存到ArrayList<Integer>中,返回即可。
*
* @param totalMoney 总金额为方便计算,已经转换为整数,单位为分。
* @param totalCount 红包个数
* @return ArrayList<Integer> 元素为各个红包的金额值,所有元素的值累和等于总金额。
*/
ArrayList<Integer> divide(int totalMoney, int totalCount);
}
package cn.itcast.day11.red;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 红包的框架 RedPacketFrame
*
* AWT / Swing / JavaFX
*
* @author 不是我
*/
public abstract class RedPacketFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final String DIR = "day11-code\\pic";
private ArrayList<Integer> moneyList = null;
private static int initMoney = 0;
private static int totalMoney = 0; // 单位为“分”
private static int count = 0;
private static HashMap<JPanel, JLabel> panelLable = new HashMap<>();
// 设置字体
private static Font fontYaHei = new Font("微软雅黑", Font.BOLD, 20);
private static Font msgFont = new Font("微软雅黑", Font.BOLD, 20);
private static Font totalShowFont = new Font("微软雅黑", Font.BOLD, 40);
private static Font nameFont = new Font("微软雅黑", Font.BOLD, 40);
private static Font showNameFont = new Font("微软雅黑", Font.BOLD, 20);
private static Font showMoneyFont = new Font("微软雅黑", Font.BOLD, 50);
private static Font showResultFont = new Font("微软雅黑", Font.BOLD, 15);
/**
* 窗体大小 WIDTH:400 HEIGHT:600
*/
private static final int FRAME_WIDTH = 416; // 静态全局窗口大小
private static final int FRAME_HEIGHT = 650;
private static JLayeredPane layeredPane = null;
/// private static JPanel contentPane = null;
/**
* page1:输入页面 - InputPanel . 组件和初始化!
*/
private static JPanel inputPanel = new JPanel();
// private static JTextField input_total = new JTextField("200"); // 测试用
// private static JTextField input_count = new JTextField("3"); // 测试用
private static JTextField input_total = new JTextField();
private static JTextField input_count = new JTextField();
private static JTextField input_people = new JTextField("30");
private static JTextField input_msg = new JTextField("恭喜发财 , 大吉大利");
private static JTextField input_total_show = new JTextField("$ " + input_total.getText().trim());
private static JLabel input_inMoney = new JLabel(); // 不可见
private static JLabel input_bg_label = new JLabel(new ImageIcon(DIR + "\\01_input.jpg"));
static {
// 设置位置
input_total.setBounds(200, 90, 150, 50);
input_count.setBounds(200, 215, 150, 50);
input_people.setBounds(90, 275, 25, 30);
input_msg.setBounds(180, 340, 200, 50);
input_total_show.setBounds(130, 430, 200, 80);
input_inMoney.setBounds(10, 535, 380, 65);
input_bg_label.setBounds(0, 0, 400, 600); // 背景
// 设置字体
input_total.setFont(fontYaHei);
input_count.setFont(fontYaHei);
input_people.setFont(fontYaHei);
input_msg.setFont(msgFont);
input_msg.setForeground(new Color(255, 233, 38)); // 字体颜色 为金色
input_total_show.setFont(totalShowFont);
input_inMoney.setFont(fontYaHei);
// 透明
input_people.setOpaque(false);
input_total_show.setOpaque(false);
// 编 辑 -- 不可编辑
input_people.setEditable(false);
input_total_show.setEditable(false);
// 边界 -- 无
input_total.setBorder(null);
input_count.setBorder(null);
input_people.setBorder(null);
input_msg.setBorder(null);
input_total_show.setBorder(null);
}
/**
* page2:打开页面 - openPanel . 组件和初始化!
*/
private static JPanel openPanel = new JPanel();
private static JTextField open_ownerName = new JTextField("谁谁谁");
private static JLabel open_label = new JLabel(new ImageIcon(DIR + "\\02_open_2.gif"));
private static JLabel open_bg_label = new JLabel(new ImageIcon(DIR + "\\02_open_1.jpg"));
static {
// 设置 位置.
open_ownerName.setBounds(0, 110, 400, 50);
open_bg_label.setBounds(0, 0, 400, 620);
open_label.setBounds(102, 280, 200, 200);
open_ownerName.setHorizontalAlignment(JTextField.CENTER);
// 设置字体
open_ownerName.setFont(nameFont);
open_ownerName.setForeground(new Color(255, 200, 163)); // 字体颜色 为金色
// 背景色
// open_name.setOpaque(false);
open_ownerName.setBackground(new Color(219, 90, 68));
// 不可编辑
open_ownerName.setEditable(false);
// 边框
open_ownerName.setBorder(null);
}
/**
* page3:展示页面 - showPanel . 组件和初始化!
*/
private static JPanel showPanel = new JPanel();
private static JPanel showPanel2 = new JPanel();
private static JScrollPane show_jsp = new JScrollPane(showPanel2);
private static JLabel show_bg_label = new JLabel(new ImageIcon(DIR + "\\03_money_1.jpg"));
private static JTextField show_name = new JTextField("用户名称");
private static JTextField show_msg = new JTextField("祝福信息");
private static JTextField show_money = new JTextField("99.99");
private static JTextField show_result = new JTextField(count + "个红包共" + (totalMoney / 100.0) + "元,被抢光了");
static {
// 分别设置水平和垂直滚动条自动出现
// jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
/*
* 两部分 页面 . 1.本人获得的红包-- showPanel 2.别人获得的红包-- show_jsp
*/
show_name.setBounds(125, 180, 100, 30);
show_name.setOpaque(false);
show_name.setBorder(null);
show_name.setFont(showNameFont);
show_msg.setBounds(0, 220, 400, 30);
show_msg.setOpaque(false);
show_msg.setBorder(null);
show_msg.setFont(msgFont);
show_msg.setHorizontalAlignment(JTextField.CENTER);
show_money.setBounds(0, 270, 250, 40);
show_money.setOpaque(false);
show_money.setBorder(null);
show_money.setFont(showMoneyFont);
show_money.setForeground(new Color(255, 233, 38)); // 字体颜色 为金色
show_money.setHorizontalAlignment(SwingConstants.RIGHT);
show_result.setBounds(10, 460, 400, 20);
show_result.setOpaque(false);
show_result.setBorder(null);
show_result.setFont(showResultFont);
show_result.setForeground(new Color(170, 170, 170)); // 字体颜色 为灰色
// 设置 图片.
show_bg_label.setBounds(0, 0, 400, 500);
}
static {
// 页面和 背景的对应关系.
panelLable.put(inputPanel, input_bg_label);
panelLable.put(openPanel, open_bg_label);
panelLable.put(showPanel, show_bg_label);
}
private void init() {
// 层次面板-- 用于设置背景
layeredPane = this.getLayeredPane();
// System.out.println("层次面板||" + layeredPane);
// System.out.println(layeredPane);
// 初始化框架 -- logo 和基本设置
initFrame();
// 初始化 三个页面 -- 准备页面
initPanel();
// 2.添加 页面 --第一个页面, 输入 panel 设置到 页面上.
setPanel(inputPanel);
// 3.添加 监听
addListener();
}
/**
* 初始化框架 -- logo 和基本设置
*/
private void initFrame() {
// logo
this.setIconImage(Toolkit.getDefaultToolkit().getImage(DIR + "\\logo.gif"));
// System.out.println("LOGO初始化...");
// 窗口设置
this.setSize(FRAME_WIDTH, FRAME_HEIGHT); // 设置界面大小
this.setLocation(280, 30); // 设置界面出现的位置
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
// 测试期 注释 拖 拽 , 运行放开
// this.setResizable(false);
this.setVisible(true);
}
/**
* 初始化页面-- 准备三个页面
*/
private void initPanel() {
// System.out.println("页面初始化...");
initInputPanel();
initOpenPanel();
initShowPanel();
}
private void initInputPanel() {
inputPanel.setLayout(null);
inputPanel.setBounds(0, -5, 400, 600);
// this.add(bg_label);
inputPanel.add(input_total);
inputPanel.add(input_count);
inputPanel.add(input_people);
inputPanel.add(input_msg);
inputPanel.add(input_total_show);
inputPanel.add(input_inMoney);
// System.out.println("输入页面||" + inputPanel);
}
private void initOpenPanel() {
openPanel.setLayout(null);
openPanel.setBounds(0, 0, 400, 600);
// this.add(bg_label);
openPanel.add(open_ownerName);
openPanel.add(open_label);
// System.out.println("打开页面||" + openPanel);
}
private void initShowPanel() {
showPanel.setLayout(null);
showPanel.setBounds(10, 10, 300, 600);
// ==============
showPanel.add(show_name);
showPanel.add(show_msg);
showPanel.add(show_money);
showPanel.add(show_result);
// System.out.println("展示页面||" + showPanel);
// ====================================
// showPanel2.setLayout(null);
// showPanel2.setBounds(0, 500, 401, 300);
showPanel2.setPreferredSize(new Dimension(300, 1000));
showPanel2.setBackground(Color.white);
show_jsp.setBounds(0, 500, 400, 110);
}
/**
* 每次打开页面, 设置 panel的方法
*/
private void setPanel(JPanel panel) {
// 移除当前页面
layeredPane.removeAll();
// System.out.println("重新设置:新页面");
// 背景lable添加到layeredPane的默认层
layeredPane.add(panelLable.get(panel), JLayeredPane.DEFAULT_LAYER);
// 面板panel设置为透明
panel.setOpaque(false);
// 面板panel 添加到 layeredPane的modal层
layeredPane.add(panel, JLayeredPane.MODAL_LAYER);
}
// private void setShowPanel(JPanel show) {
// setPanel(show);
// layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);
//
// }
/**
* 设置组件的监听器
*/
private void addListener() {
input_total.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// System.out.println(e);
String input_total_money = input_total.getText();
input_total_show.setText("$ " + input_total_money);
}
});
input_count.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// System.out.println(e);
// System.out.println("个数:" + input_count.getText());
}
});
input_msg.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// System.out.println(e);
// System.out.println("留言:" + input_msg.getText());
}
});
input_inMoney.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
// 获取页面的值.
totalMoney = (int) (Double.parseDouble(input_total.getText()) * 100); // 转换成"分"
count = Integer.parseInt(input_count.getText());
if (count > 30) {
JOptionPane.showMessageDialog(null, "红包个数不得超过30个", "红包个数有误", JOptionPane.INFORMATION_MESSAGE);
return;
}
initMoney = totalMoney;
System.out.println("总金额:[" + totalMoney + "]分");
System.out.println("红包个数:[" + count + "]个");
input_inMoney.removeMouseListener(this);
// System.out.println("跳转-->打开新页面");
// 设置群主名称
open_ownerName.setText(ownerName);
// 设置打开页面
setPanel(openPanel);
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "请输入正确【总金额】或【红包个数】", "输入信息有误", JOptionPane.ERROR_MESSAGE);
}
}
});
// open_ownerName ,点击 [名称],触发的方法 , 提示如何设置群主名称.
open_ownerName.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请通过【setOwnerName】方法设置群主名称", "群主名称未设置",
JOptionPane.QUESTION_MESSAGE);
}
});
// open label , 点击 [开],触发的方法,提示如何设置打开方式.
open_label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (openWay == null) {
JOptionPane.showMessageDialog(null, "请通过【setOpenWay】方法设置打开方式", "打开方式未设置",
JOptionPane.QUESTION_MESSAGE);
return;
}
// System.out.println("跳转-->展示页面");
moneyList = openWay.divide(totalMoney, count);
// System.out.println(moneyList);
/*
* showPanel 添加数据
*
*/
show_name.setText(ownerName);
show_msg.setText(input_msg.getText());
if (moneyList.size() > 0) {
show_money.setText(moneyList.get(moneyList.size() - 1) / 100.0 + "");
}
show_result.setText(count + "个红包共" + (initMoney / 100.0) + "元,被抢光了");
open_label.removeMouseListener(this);
setPanel(showPanel);
// 添加数据
for (int i = 0; i < moneyList.size(); i++) {
JTextField tf = new JTextField();
tf.setBorder(null);
tf.setFont(showNameFont);
tf.setHorizontalAlignment(JTextField.LEFT);
if (i == moneyList.size() - 1) {
tf.setText(ownerName + ":\t" + moneyList.get(i) / 100.0 + "元");
} else {
tf.setText("群成员-" + i + ":\t" + moneyList.get(i) / 100.0 + "元");
}
showPanel2.add(tf);
}
layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);
}
});
}
/* ======================================================================
* **********************************************************************
* * 以上代码均为页面部分处理,包括布局/互动/跳转/显示等,大家 *
* * *
* * *
* **********************************************************************
* ======================================================================
*/
/**
* ownerName : 群主名称
*/
private String ownerName = "谁谁谁"; // 群主名称
/**
* openWay : 红包的类型 [普通红包/手气红包]
*/
private OpenMode openWay = null;
/**
* 构造方法:生成红包界面。
*
* @param title 界面的标题
*/
public RedPacketFrame(String title) {
super(title);
// 页面相关的初始化
init();
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public void setOpenWay(OpenMode openWay) {
this.openWay = openWay;
}
}
package cn.itcast.day11.demo08;
import cn.itcast.day11.red.RedPacketFrame;
public class MyRed extends RedPacketFrame {
/**
* 构造方法:生成红包界面。
*
* @param title 界面的标题
*/
public MyRed(String title) {
super(title);
}
}
package cn.itcast.day11.demo08;
import cn.itcast.day11.red.OpenMode;
import java.util.ArrayList;
public class NormalMode implements OpenMode {
@Override
public ArrayList<Integer> divide(final int totalMoney, final int totalCount) {
ArrayList<Integer> list = new ArrayList<>();
int avg = totalMoney / totalCount; // 平均值
int mod = totalMoney % totalCount; // 余数,模,零头
// 注意totalCount - 1代表,最后一个先留着
for (int i = 0; i < totalCount - 1; i++) {
list.add(avg);
}
// 有零头,需要放在最后一个红包当中
list.add(avg + mod);
return list;
}
}
package cn.itcast.day11.demo08;
import cn.itcast.day11.red.OpenMode;
import java.util.ArrayList;
import java.util.Random;
public class RandomMode implements OpenMode {
@Override
public ArrayList<Integer> divide(final int totalMoney, final int totalCount) {
ArrayList<Integer> list = new ArrayList<>();
// 随机分配,有可能多,有可能少。
// 最少1分钱,最多不超过“剩下金额平均数的2倍”
// 第一次发红包,随机范围是0.01元~6.66元
// 第一次发完之后,剩下的至少是3.34元。
// 此时还需要再发2个红包
// 此时的再发范围应该是0.01元~3.34元(取不到右边,剩下0.01)
// 总结一下,范围的【公式】是:1 + random.nextInt(leftMoney / leftCount * 2);
Random r = new Random(); // 首先创建一个随机数生成器
// totalMoney是总金额,totalCount是总份数,不变
// 额外定义两个变量,分别代表剩下多少钱,剩下多少份
int leftMoney = totalMoney;
int leftCount = totalCount;
// 随机发前n-1个,最后一个不需要随机
for (int i = 0; i < totalCount - 1; i++) {
// 按照公式生成随机金额
int money = r.nextInt(leftMoney / leftCount * 2) + 1;
list.add(money); // 将一个随机红包放入集合
leftMoney -= money; // 剩下的金额越发越少
leftCount--; // 剩下还应该再发的红包个数,递减
}
// 最后一个红包不需要随机,直接放进去就得了
list.add(leftMoney);
return list;
}
}
package cn.itcast.day11.demo08;
import cn.itcast.day11.red.OpenMode;
/*
场景说明:
红包发出去之后,所有人都有红包,大家抢完了之后,最后一个红包给群主自己。
大多数代码都是现成的,我们需要做的就是填空题。
我们自己要做的事情有:
1. 设置一下程序的标题,通过构造方法的字符串参数
2. 设置群主名称
3. 设置分发策略:平均,还是随机?
红包分发的策略:
1. 普通红包(平均):totalMoney / totalCount,余数放在最后一个红包当中。
2. 手气红包(随机):最少1分钱,最多不超过平均数的2倍。应该越发越少。
*/
public class Bootstrap {
public static void main(String[] args) {
MyRed red = new MyRed("传智播客双元课程");
// 设置群主名称
red.setOwnerName("王思聪");
// 普通红包
// OpenMode normal = new NormalMode();
// red.setOpenWay(normal);
// 手气红包
OpenMode random = new RandomMode();
red.setOpenWay(random);
}
}
12.JDK9的JShell简单使用
13.编译器的两点优化