為了講明白繼承和super、this關鍵字,群主發了20塊錢群紅包

摘要:以群主發紅包為例,帶你深入了解繼承和super、this關鍵字 。
本文分享自華為云社區《群主發紅包帶你深入了解繼承和super、this關鍵字》,作者:共飲一杯無。
需求群主發隨機紅包或者普通紅包 。某群有多名成員,群主給成員發普通紅包 。
隨機紅包規則:
  1. 群主的一筆金額,從群主余額中扣除,隨機分成n等份,讓成員領取 。
  2. 成員領取紅包后 , 保存到成員余額中 。
普通紅包的規則:
  1. 群主的一筆金額,從群主余額中扣除,平均分成n等份,讓成員領取 。
  2. 成員領取紅包后 , 保存到成員余額中 。
案例分析案例分析,可以得出如下繼承關系:
為了講明白繼承和super、this關鍵字,群主發了20塊錢群紅包

文章插圖
案例代碼實現定義用戶類/** * 用戶類 * @author zjq */public class User { /*** 姓名*/ private String name; /*** 余額,也就是當前用戶擁有的錢數*/ private Integer money; public User() { } public User(String name, Integer money) { this.name = name; this.money = money; } // 展示一下當前用戶有多少錢 public void show() { System.out.println("我是" + name + ",我有多少錢:" + this.fenToYuan(String.valueOf(money))+"元"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getMoney() { return money; } public void setMoney(Integer money) { this.money = money; } /*** 分轉元* @param amount* @return*/ public String fenToYuan(String amount){ NumberFormat format = NumberFormat.getInstance(); try{ Number number = format.parse(amount); double temp = number.doubleValue() / 100.0; format.setGroupingUsed(false); format.setMaximumFractionDigits(2);amount = format.format(temp); } catch (ParseException e){ e.printStackTrace(); } return amount; }}定義群主類package com.zjq.javabase.base09.demo14;import org.apache.commons.lang3.RandomUtils;import java.util.ArrayList;/** * 群主的類 * @author zjq */public class Manager extends User { /*** 收到單個紅包最大值*/ private static final int MAX_AMOUNT = 20000; public Manager() { } public Manager(String name, int money) { // 通過super 調用父類構造方法 super(name, money); } /*** 發紅包* @param totalMoney 紅包總金額(單位分)* @param count 發包個數* @param type 發包類型(0、隨機紅包,1、定額紅包)* @return 紅包集合* @throws Exception*/ public ArrayList<Integer> send(Integer totalMoney, int count,int type) throws Exception { // 首先需要一個集合,用來存儲若干個紅包的金額 ArrayList<Integer> redList = new ArrayList<>(count); // 首先看一下群主自己有多少錢 Integer leftMoney = super.getMoney(); // 群主當前余額 if (totalMoney > leftMoney) { System.out.println("余額不足"); return redList; // 返回空集合 } // 扣錢 , 其實就是重新設置余額 super.setMoney(leftMoney - totalMoney); if (count == 1) { redList.add(totalMoney); return redList; } switch (type) { case 0: // 默認分配1分至每一位 for (int i = 0; i < count; i++) { redList.add(1); } int surplus_currency = totalMoney - redList.size(),// 剩余金額數 surplus_number = redList.size();// 剩余需追加的數量 for (int i = 0; i < redList.size(); i++) { // 沒值可以追加了 if (new Integer(0).equals(surplus_currency)) { break; } // (總數-(總包-i)*最小值) / (總包 - i) 隨機安全值算法 int safe_total = (int)Math.floor((totalMoney - (count - i)) / (count - i)); if (new Integer(0).equals(safe_total)) {// 隨機值不能為0 safe_total = 1; } // 該次隨機值 int randomint = surplus_currency >= safe_total - 1 ? safe_total : surplus_currency + 1; // 下次可能最大能剩余值 int nextMax_currency = (MAX_AMOUNT - 1) * (surplus_number - 1); // 最小的隨機數 剩余金額-剩余最大隨機的總數(不含這一次) int minRandom = surplus_currency - nextMax_currency; if (minRandom < 0) { minRandom = 0; } // 規避一些特殊情況,每個接近2000或1時會發生 boolean must = (surplus_currency - count * MAX_AMOUNT <= 2 && surplus_currency - count * MAX_AMOUNT >= 0) /*|| surplus_currency < packet_number * 2*/; // 控制安全隨機值 隨機安全值不能大于最大限制 , 并且不能小于最小限 制 if (safe_total < minRandom || safe_total > MAX_AMOUNT || must) { safe_total = MAX_AMOUNT; // 該次隨機值 randomint = surplus_currency >= safe_total - 1 ? safe_total : surplus_currency + 1; // 下次可能最大能剩余值 nextMax_currency = (randomint - 1) * (surplus_number - 1); // 最小的隨機數 剩余金額-剩余最大隨機的總數(不含這一次) minRandom = surplus_currency - nextMax_currency; if (minRandom < 0) { minRandom = 0; } } // 下一次最大的隨機值 int nextMaxRandomInt = nextMax_currency - (surplus_currency - (randomint - 1)); Integer maxRandom = nextMaxRandomInt <= 0 ? nextMaxRandomInt + randomint: null; // 能隨機 剩余的金額- 最大隨機數 >最大隨機數* 剩余數量 boolean canRandom = surplus_currency - (randomint - 1) > nextMax_currency || nextMaxRandomInt > (randomint - 1) || !new Integer(0).equals(minRandom); int addNumber; // 追加的金額 if (canRandom && !new Integer(randomint).equals(minRandom+1) && !(new Integer(randomint).equals(minRandom) && new Integer(safe_total).equals(minRandom)) ) { addNumber = myRandom(minRandom, maxRandom == null ? randomint : maxRandom- 1); }else { addNumber = randomint - 1; } redList.set(i,redList.get(i) + addNumber); surplus_currency -= addNumber; surplus_number--; } break; case 1: // 定額紅包校驗 redList = new ArrayList<>(count); for (int i = 0; i <count; i++) { //定額紅包要是不能整除會有問題,正常實現應該是輸入單個紅包金額和總數直接就能計算 redList.add(totalMoney/count); } break; default: throw new Exception("類型錯誤!"); } System.out.println("我是" + this.getName() + "我發了"+fenToYuan(String.valueOf(totalMoney))+"元紅包"+",我現在有多少錢:" + fenToYuan(String.valueOf(this.getMoney()))+"元"); return redList; } /*** 生成隨機金額* @param min* @param randomint* @return*/ public static int myRandom(int min,int randomint) { if (min == 0) { return RandomUtils.nextInt(0,randomint); }else { int nextInt = RandomUtils.nextInt(min,randomint - min); return nextInt + min; } }}

推薦閱讀