Java工具集-數學(對數函數)

  • 2019 年 10 月 26 日
  • 筆記

簡單工具類

寫作初衷:由於日常開發經常需要用到很多工具類,經常根據需求自己寫也比較麻煩 網上好了一些工具類例如commom.lang3或者hutool或者Jodd這樣的開源工具,但是 發現他們之中雖然設計不錯,但是如果我想要使用,就必須要引入依賴並且去維護依賴,有些 甚至會有存在版本編譯不通過問題,故此想要寫作一個每個類都可以作為獨立工具類使用 每個使用者只需要複製該類,到任何項目當中都可以使用,所以需要尊從以下兩個原則才能 做到.在此誠邀各位大佬參與.可以把各自用過的工具,整合成只依賴JDK,每個類都能夠單獨 使用的工具.每個人當遇到業務需求需要使用的時候,只需要到這裡單獨拷貝一個即可使用. 拋棄傳統的需要引入依賴的煩惱.讓大家一起來解決你所面臨的業務問題吧!

介紹

遵從兩大原則

  • 1.絕不依賴JDK以外的源碼
  • 2.犧牲程式碼復用性,每個類都必須是單獨的組件,絕不互相引用,做到完全解耦
package *;    /**   * @program: simple_tools   * @description: 對數函數   * @author: ChenWenLong   * @create: 2019-10-24 19:57   **/  public class LogFunction {        //y=logaX (a>0,且a≠1)      private double a = 2;      private static final Point DEFAULT_POINT = new Point(1,0);      public static LogFunction instance;        private LogFunction() {      }        static {          if (instance == null) {              synchronized (LogFunction.class) {                  if (instance == null) {                      instance = new LogFunction();                  }              }          }      }        /**       * 功能描述:       * 〈初始化對數函數〉       *       * @return : void       * @params : [a]       * @author : cwl       * @date : 2019/10/25 9:11       */      public static void init(double a) {          if (a < 0 || a == 1) {              throw new RuntimeException("a is not less than zero and not be one");          }          instance.setA(a);      }        /**       * 功能描述:       * 〈判斷點是否在對數函數上〉       *       * @params : [point]       * @return : boolean       * @author : cwl       * @date : 2019/10/25 9:18       */      public static boolean isOnline(Point point) {          if (point == null){              throw new RuntimeException("point is not be null");          }          double x = point.getX();          double y = point.getY();          return y == Math.pow(x,instance.getA());      }        /**       * 功能描述:       * 〈每個對數函數都會過點(1,0)〉       *       * @params : []       * @return : com.simple.util.math.function.LogFunction.Point       * @author : cwl       * @date : 2019/10/25 9:23       */      public static Point getDefaultPoint(){          return DEFAULT_POINT;      }        // 二維函數圖上的點      public static class Point {          // 坐標 x          private double x;          // 坐標 y          private double y;            public Point(double x,double y){              this.x = x;              this.y = y;          }            public double getX() {              return x;          }            public void setX(double x) {              this.x = x;          }            public double getY() {              return y;          }            public void setY(double y) {              this.y = y;          }      }        public double getA() {          return a;      }        public void setA(double a) {          this.a = a;      }  }