Skip to content

Latest commit

 

History

History
31 lines (30 loc) · 864 Bytes

Java_SE-创建Utils工具类的最佳实践.md

File metadata and controls

31 lines (30 loc) · 864 Bytes
title url tags categories date
创建Utils工具类的最佳实践
The_Best_practice_for_create_Utils_class
最佳实践
Java SE
2017-09-24 06:03:37 -0700

前言

有些类我们不希望实例化, 比如 ArrayUtils。 实例化这些工具类是没有意义的。

最佳实践

  1. 使用 finalabstract 修饰 class
  2. 私有化构造函数
  3. 构造函数抛出异常
// abstract类不能直接实例化, 只能由子类实例化
// final类不能实例化
public abstract class ArrayUtils {
    // 私有化构造函数, 子类必须调用父类的构造函数, 但是又调用不了
    private ArrayUtils() {
        // 防止反射创建, 抛出异常
        throw new AssertionError("工具类不允许实例化");
    }
    // 工具方法
    public static void sort(Integer... arr){}
}