惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
宝玉的分享
宝玉的分享
I
InfoQ
P
Privacy International News Feed
V
V2EX
IT之家
IT之家
S
SegmentFault 最新的问题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
The Register - Security
The Register - Security
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
B
Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
T
Threatpost
Forbes - Security
Forbes - Security
U
Unit 42
A
Arctic Wolf
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
L
Lohrmann on Cybersecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
月光博客
月光博客
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
I
Intezer
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
MyScale Blog
MyScale Blog
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位

博客园 - 有点懒惰的大青年

用一个实际业务场景演示ON和WHERE的正确使用方式 idea中查看源码 异或运算 使用Hutool对时间的花式操作 合并K个升序列表 比较器 spring boot中使用RedissonClient实现分布式锁 idea同时启动application,启用不同端口 @PostConstruct用法 ApplicationContext 事件发布与监听机制详解 java自带命令jps介绍 关于maven中标签的说明 mysql的跨库查询 linux命令ll显示结果的含义 Files类的使用 java环境变量设置 java值传递和引用传递 kafka的集群与可靠性 kafka的消费全流程 关于kafka 达梦数据库执行计划介绍 关于MQ 用位运算实现加减乘除(3)
枚举类的设计模式
有点懒惰的大青年 · 2026-03-08 · via 博客园 - 有点懒惰的大青年

一、TimeUnit设计模式

当像时间种类这样的,自身属性很少,类似这种时间单位的,又有很多种不同类型的,看到TimeUnit的实现方式,可以参考来实现。下面是我自己的模拟代码,模拟jdk源码中的TimeUnit设计模式:

AnimalCategory,动物类型枚举类:

package com.test.enums;

import lombok.extern.slf4j.Slf4j;

/**
 * 动物类型枚举类
 */
@Slf4j
public enum AnimalCategory {

    LION {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 60; }
        String getFamily() { return "猫科"; }
        String getEater() { return "肉食"; }
    },

    TIGER {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 70; }
        String getFamily() { return "猫科"; }
        String getEater() { return "肉食"; }
    },

    WOLF {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 50; }
        String getFamily() { return "犬科"; }
        String getEater() { return "杂食"; }
    };

    /**
     * 科属,猫科还是犬科等
     */
    abstract String getFamily();

    /**
     * 食性,肉食或素食或杂食
     */
    abstract String getEater();

    /**
     * 动物的吼叫声
     */
    public void bellow(String msg) {
        throw new AbstractMethodError();
    }

    /**
     * 动物的奔跑时速
     */
    abstract int runningSpeed();
}

TestAnimalsCategory,测试不同的动物物种的表现:

package com.test.enums;

import lombok.extern.slf4j.Slf4j;

/**
 * 测试不同的动物物种的表现
 */
@Slf4j
public class TestAnimalsCategory {
    public static void main(String[] args) {
        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.LION.name(), AnimalCategory.LION.runningSpeed(),
                AnimalCategory.LION.getFamily(), AnimalCategory.LION.getEater());
        AnimalCategory.LION.bellow("你在哪?");


        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.TIGER.name(), AnimalCategory.TIGER.runningSpeed(),
                AnimalCategory.TIGER.getFamily(), AnimalCategory.TIGER.getEater());
        AnimalCategory.TIGER.bellow("她在哪?");

        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.WOLF.name(), AnimalCategory.WOLF.runningSpeed(),
                AnimalCategory.WOLF.getFamily(), AnimalCategory.WOLF.getEater());
        AnimalCategory.WOLF.bellow("月色不错?");
    }
}

打印结果:

13:30:51.062 [main] INFO com.test.enums.TestAnimalsCategory - LION的奔跑速度为60km/h,是猫科动物,食性为肉食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - LION的低吼:你在哪?
13:30:51.066 [main] INFO com.test.enums.TestAnimalsCategory - TIGER的奔跑速度为70km/h,是猫科动物,食性为肉食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - TIGER的低吼:她在哪?
13:30:51.066 [main] INFO com.test.enums.TestAnimalsCategory - WOLF的奔跑速度为50km/h,是犬科动物,食性为杂食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - WOLF的低吼:月色不错?

--