Java枚举以及实现原理

Java EnumSet工作原理初窥 EnumSet是Java枚举类型的泛型容器,Java既然有了SortedSet、TreeSet、HashSet等容器,为何还要多一个EnumSet<T>呢?答案肯定是EnumSet有一定的特性,举个例子,EnumSet的速度很快。其他特性不一一列举了,毕竟本文的内容不是介绍EnumSet的特性。   首先以事实说话,存在这样一个EnumSet,它有50个枚举值T0~T49,将50个值插入到容器(HashSet、EnumSet)中,为一个操作,将50个枚举值移出做为第二个操作。把第一个和第二个操作执行的总时间设定为一个周期,拿HashSet操作的一个周期和EnumSet的一个周期做比较自然没什 立即下载

不要让这个世界的复杂性阻碍你的前进。 要成为一个行动主义者,将解决人类的不平等视为己任。 它将成为你生命中最重要的经历之一。
——比尔·盖茨在哈佛大学的演讲

声明枚举

[public]enum 枚举类型名称[implements 接口名称列表]
{
	枚举值;
	变量成员声明及初始化;
	方法声明及方法体;
}

简单的例子:

/**
 * 简单的枚举类型举例
 * @author wangbaofu
 * 
 */
public class ScoreTester {
	public static void main(String[] args) {
		giveScore(Score.EXCELLENT);
	}
	private static void giveScore(Score s) {
		switch (s) {
		case EXCELLENT:
			System.out.println("Excellent");
			break;
		case QUALIFIED:
			System.out.println("Qualified");
			break;
		case FAILED:
			System.out.println("Failed");
			break;
		default:
			break;
		}
	}
}
enum Score {
	EXCELLENT, QUALIFIED, FAILED;
}
/*
 * 输出 Excellent
 * */

通过jad反编译一下:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ScoreTester.java

package enum_;


final class Score extends Enum
{

    private Score(String s, int i)
    {
        super(s, i);
    }

    public static Score[] values()
    {
        Score ascore[];
        int i;
        Score ascore1[];
        System.arraycopy(ascore = ENUM$VALUES, 0, ascore1 = new Score[i = ascore.length], 0, i);
        return ascore1;
    }

    public static Score valueOf(String s)
    {
        return (Score)Enum.valueOf(enum_/Score, s);
    }

    public static final Score EXCELLENT;
    public static final Score QUALIFIED;
    public static final Score FAILED;
    public static final Score ADD;
    private static final Score ENUM$VALUES[];

    static 
    {
        EXCELLENT = new Score("EXCELLENT", 0);
        QUALIFIED = new Score("QUALIFIED", 1);
        FAILED = new Score("FAILED", 2);
        ADD = new Score("ADD", 3);
        ENUM$VALUES = (new Score[] {
            EXCELLENT, QUALIFIED, FAILED, ADD
        });
    }
}

枚举的特点:

  • 枚举定义实际上是定义了一个类
  • 所有枚举类型都隐含继承(扩展)自Java.lang.Enum,因此枚举类型不能再继承任何其它类
  • 枚举类型中的类可以包括方法和变量
  • 枚举类型的构造方法必须是包内私有或者私有的。定义在枚举开头的常量会自动创建,不能显示地调用枚举类的构造方法。

枚举类型的默认方法

  • 静态的values()方法用于获得枚举类型的枚举值的数组
  • toString方法返回枚举值的字符串描述
  • valueOf方法将以字符串形式表示的枚举值转化为枚举类型的对象
  • Ordinal方法获得对象在枚举类型中的位置索引

最佳实践 (该部分内容为《编写高质量代码之java》学习笔记)

下边贴一下jdk中Enum的源码

/*
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang;

import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;

/**
 * This is the common base class of all Java language enumeration types.
 *
 * More information about enums, including descriptions of the
 * implicitly declared methods synthesized by the compiler, can be
 * found in section 8.9 of
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * <p> Note that when using an enumeration type as the type of a set
 * or as the type of the keys in a map, specialized and efficient
 * {@linkplain java.util.EnumSet set} and {@linkplain
 * java.util.EnumMap map} implementations are available.
 *
 * @param <E> The enum type subclass
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see     Class#getEnumConstants()
 * @see     java.util.EnumSet
 * @see     java.util.EnumMap
 * @since   1.5
 */
public abstract class Enum<E extends Enum<E>>
        implements Comparable<E>, Serializable {
    /**
     * The name of this enum constant, as declared in the enum declaration.
     * Most programmers should use the {@link #toString} method rather than
     * accessing this field.
     */
    private final String name;

    /**
     * Returns the name of this enum constant, exactly as declared in its
     * enum declaration.
     *
     * <b>Most programmers should use the {@link #toString} method in
     * preference to this one, as the toString method may return
     * a more user-friendly name.</b>  This method is designed primarily for
     * use in specialized situations where correctness depends on getting the
     * exact name, which will not vary from release to release.
     *
     * @return the name of this enum constant
     */
    public final String name() {
        return name;
    }

    /**
     * The ordinal of this enumeration constant (its position
     * in the enum declaration, where the initial constant is assigned
     * an ordinal of zero).
     *
     * Most programmers will have no use for this field.  It is designed
     * for use by sophisticated enum-based data structures, such as
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     */
    private final int ordinal;

    /**
     * Returns the ordinal of this enumeration constant (its position
     * in its enum declaration, where the initial constant is assigned
     * an ordinal of zero).
     *
     * Most programmers will have no use for this method.  It is
     * designed for use by sophisticated enum-based data structures, such
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     *
     * @return the ordinal of this enumeration constant
     */
    public final int ordinal() {
        return ordinal;
    }

    /**
     * Sole constructor.  Programmers cannot invoke this constructor.
     * It is for use by code emitted by the compiler in response to
     * enum type declarations.
     *
     * @param name - The name of this enum constant, which is the identifier
     *               used to declare it.
     * @param ordinal - The ordinal of this enumeration constant (its position
     *         in the enum declaration, where the initial constant is assigned
     *         an ordinal of zero).
     */
    protected Enum(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }

    /**
     * Returns the name of this enum constant, as contained in the
     * declaration.  This method may be overridden, though it typically
     * isn't necessary or desirable.  An enum type should override this
     * method when a more "programmer-friendly" string form exists.
     *
     * @return the name of this enum constant
     */
    public String toString() {
        return name;
    }

    /**
     * Returns true if the specified object is equal to this
     * enum constant.
     *
     * @param other the object to be compared for equality with this object.
     * @return  true if the specified object is equal to this
     *          enum constant.
     */
    public final boolean equals(Object other) {
        return this==other;
    }

    /**
     * Returns a hash code for this enum constant.
     *
     * @return a hash code for this enum constant.
     */
    public final int hashCode() {
        return super.hashCode();
    }

    /**
     * Throws CloneNotSupportedException.  This guarantees that enums
     * are never cloned, which is necessary to preserve their "singleton"
     * status.
     *
     * @return (never returns)
     */
    protected final Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    /**
     * Compares this enum with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * Enum constants are only comparable to other enum constants of the
     * same enum type.  The natural order implemented by this
     * method is the order in which the constants are declared.
     */
    public final int compareTo(E o) {
        Enum<?> other = (Enum<?>)o;
        Enum<E> self = this;
        if (self.getClass() != other.getClass() && // optimization
            self.getDeclaringClass() != other.getDeclaringClass())
            throw new ClassCastException();
        return self.ordinal - other.ordinal;
    }

    /**
     * Returns the Class object corresponding to this enum constant's
     * enum type.  Two enum constants e1 and  e2 are of the
     * same enum type if and only if
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
     * (The value returned by this method may differ from the one returned
     * by the {@link Object#getClass} method for enum constants with
     * constant-specific class bodies.)
     *
     * @return the Class object corresponding to this enum constant's
     *     enum type
     */
    @SuppressWarnings("unchecked")
    public final Class<E> getDeclaringClass() {
        Class<?> clazz = getClass();
        Class<?> zuper = clazz.getSuperclass();
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
    }

    /**
     * Returns the enum constant of the specified enum type with the
     * specified name.  The name must match exactly an identifier used
     * to declare an enum constant in this type.  (Extraneous whitespace
     * characters are not permitted.)
     *
     * <p>Note that for a particular enum type {@code T}, the
     * implicitly declared {@code public static T valueOf(String)}
     * method on that enum may be used instead of this method to map
     * from a name to the corresponding enum constant.  All the
     * constants of an enum type can be obtained by calling the
     * implicit {@code public static T[] values()} method of that
     * type.
     *
     * @param <T> The enum type whose constant is to be returned
     * @param enumType the {@code Class} object of the enum type from which
     *      to return a constant
     * @param name the name of the constant to return
     * @return the enum constant of the specified enum type with the
     *      specified name
     * @throws IllegalArgumentException if the specified enum type has
     *         no constant with the specified name, or the specified
     *         class object does not represent an enum type
     * @throws NullPointerException if {@code enumType} or {@code name}
     *         is null
     * @since 1.5
     */
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                                String name) {
        T result = enumType.enumConstantDirectory().get(name);
        if (result != null)
            return result;
        if (name == null)
            throw new NullPointerException("Name is null");
        throw new IllegalArgumentException(
            "No enum constant " + enumType.getCanonicalName() + "." + name);
    }

    /**
     * enum classes cannot have finalize methods.
     */
    protected final void finalize() { }

    /**
     * prevent default deserialization
     */
    private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
        throw new InvalidObjectException("can't deserialize enum");
    }

    private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("can't deserialize enum");
    }
}

推荐使用枚举定义常量

常量声明是每一个项目都不可或缺的,在Java 1.5之前,
我们只有两种方式的声明:类常量和接口常量,若在项目中
使用的是Java 1.5之前的版本基本上都是如此定义的。不过,
在1.5版以后有了改进,即新增了一种常量声明方式: 枚举声明常量。
枚举声明常量的优点
**(1)**枚举常量更简单
**(2)**枚举常量属于稳态型
**(3)**枚举具有内置方法
是java.lang.Enum的子类,该基类提供了诸如获得排序值的ordinal方法、compareTo比较方法等,大大简化了常量的访问。
**(4)**枚举可以自定义方法
举常量不仅可以定义静态方法,还可以定义非静态方法,而且还能够从根本上杜绝常量类被实例化。

	package enumtest;
/**
 * 枚举的优势
 *
 */
public class TestSeason {
	public static void main(String[] args) {
//		通过values方法获得所有的枚举项
		for (Season s : Season.values()) {
			System.out.println(s);
		}
	}
}
enum Season{
	Spring,Summer,Autumn,Winter;
}
interface SeasonIntece{
	int Spring=0;
	int Summer=1;
	int Autumn=2;
	int Winter=3;
}

使用构造函数协助描述枚举项

枚举的属性:排序号,其默认值为0,1,2,3…
枚举描述:含义通过枚举的构造函数,声明每个枚举项(也就是枚举的实例)
必须具有属性和行为,这是对枚举的描述和补充,目的是使,枚举项表述的意义更加清晰准确

	enum Season {
	Spring("春"), Summer("夏"), Autumn("秋"), Winter("冬");
	private String desc;

	Season(String desc) {
		this.desc = desc;
	}
	// 自定义方法
	public static Season getComfortableSeason() {
		return Spring;
	}
//获取枚举描述
	public String getDesc() {
		return desc;
	}
}	

小心switch带来的空值异常,处理方法增加空判断

对于枚举类型很多的情况下,在default中添加: throw
new AssertionError(“没有该类型”);

使用valueOf前必须进行校验

	package enumtest;

import java.util.Arrays;
import java.util.List;

public class ValueOfTest {
	public static void main(String[] args) {
		List<String> params = Arrays.asList("EXCELLENT", "aDD");
		for (String name : params) {
			// 查找直面值与name相同的枚举 项
			
//			添加代码
			if(Score.contains(name)){
				Score s = Score.valueOf(name);
				if (s != null) {
					// 有该枚举项时
					System.out.println(s);
				} else {
					// 没有该枚举项时
					System.out.println("无相关枚举项");
				}
			}
			//			Score s = Score.valueOf(name);
//			if (s != null) {
//				// 有该枚举项时
//				System.out.println(s);
//			} else {
//				// 没有该枚举项时
//				System.out.println("无相关枚举项");
//			}
		}
	}

	enum Score {
		EXCELLENT, QUALIFIED, FAILED, ADD;
		// 是否包含枚举项
		public static boolean contains(String name) {
			// 所有的枚举值
			Score[] scores = values();
			// 遍历查找
			for (Score s : scores) {
				if (s.name().equals(name))
					return true;
			}
			return false;
		}

	}
}
/*
 * Exception in thread "main" java.lang.IllegalArgumentException: No enum
 * constant enumtest.ValueOfTest.Score.aDD
 * 
 * public static<T extends Enum<T>>T valueOf(Class<T>enumType, String name){
 * //通过反射,从常量列表中查找 T result=enumType.enumConstantDirectory().get(name);
 * if(result!=null) return result; if(name==null) throw new
 * NullPointerException("Name is null"); //最后排除无效参数异常 throw new
 * IllegalArgumentException("No enum const"+enumType+"."+name); }
 */

(1) 枚举非静态方法实现工厂方法模式
用枚举实现工厂方法模式更简洁

package enumtest;

/**
 * 工厂方法模式(Factory Method Pattern)是“创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其子类”。
 * 工厂方法模式在我们的开发工作中经常会用到。
 * 这是最原始的工厂方法模式,有两个产品:福特汽车和别克汽车,然后通过工厂方法模式来生产。有了工厂方法模式,我们就不用关心一辆车具体是怎么生成的了
 * ,只要告诉工厂“给我生产一辆福特汽车”就可以了
 */
// 抽象产品
interface Car {
};

// 具体产品类
class FordCar implements Car {
};

// 具体产品类
class BuickCar implements Car {
};

// 工厂类
public class CarFactory {
	public static Car createCar(Class<? extends Car> c) {
		try {
			return c.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {

			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
//		生产车辆
		Car cat =CarFactory.createCar(FordCar.class);
	}
}
package enumtest;
//枚举非静态方法实现工厂方法模式
public class CarFactoryEnum {
	public static void main(String[] args) {
//		生产汽车
		Car car = CarFactoryEnum1.BuickCar.create();
		
	}
}

enum CarFactoryEnum1 {
	// 定义工厂类能生产汽车的类型
	FordCar, BuickCar;
	// 生产汽车
	public Car create() {
		switch (this) {
		case FordCar:
			return new FordCar();
		case BuickCar:
			return new BuickCar();
		default:
			throw new AssertionError("无效参数");
		}
	}
}

(2)通过抽象方法生成产品

enum CarFactory3{
	FordCar{
		public Car create(){
			return new FordCar();
		}
	},BuickCar{
		public Car create(){
			return new BuickCar();
		}
	};
//	首先定义一个抽象制造方法create,然后每个枚举项自行实现。
	public abstract Car create();
}

用枚举类型的工厂方法模式有以下三个优点:
(1)避免错误调用的发生
(2)性能好,使用便捷
枚举类型的计算是以int类型的计算为基础的,这是最基本的操作,性能当
然会快,至于使用便捷,注意看客户端的调用,代码的字面意思就是“汽车
工厂,我要一辆别克汽车,赶快生产”。
(3)降低类间耦合
不管生产方法接收的是Class、String还是int的参数,都会成为客户端类的
负担,这些类并不是客户端需要的,而是因为工厂方法的限制必须输入的,
例如Class参数,对客户端main方法来说,它需要传递一个FordCar.class
参数才能生产一辆福特汽车,除了在create方法中传递该参数外,业务类
不需要改Car的实现类。这严重违背了
迪米特
原则(Law of Demeter,简称为
LoD
),也就是最少知识原则:一个对象应该对其他对象有最少的了解。
示例代码



	/**

	 * 支付类型

	 */

	public enum PayType{



		typeUnknow("未知支付类型" , -1), typeAlipay("支付宝支付" , 4), typeWechatPay("微信支付" , 5);

		private String name;

		private int value;



		// 构造方法

		private PayType(String name, int value) {

			this.name = name;

			this.value = value;

		}

	}

我是IT小王,如果喜欢我的文章,可以扫码关注我的微信公众号
在这里插入图片描述

Java枚举类接口实例原理解析 主要介绍了Java枚举类接口实例原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 立即下载

相关推荐

详解各类以太网标准10BASE-T/100BASE-T4/100BASE-FX/1000BASE-X等

以太网是现实世界中最普遍的一种计算机网络。以太网有两类:第一类是经典以太网,第二类是交换式以太网,使用了一种称为交换机的设备连接不同的计算机。经典以太网是以太网的原始形式,运行速度从3-10 Mbps不等;而交换式以太网正是广泛应用的以太网,可运行在100Mbps、1000Mbps和10000Mbps高速率,分别以快速以太网、千兆以太网和万兆以太网的形式呈现

不积跬步,无以至千里! 5万+

微信小程序开发-微信支付功能【WxMaService 获取openid,WxPayService建微信订单,接收微信支付异步通知回调方法,附有完整前后端代码】

前提:对小程序开发有一定的基础;小程序已发布使用,已开通微信支付,关联商户号。

weixin_44934104的博客 8786

秋叶ComfyUI整合包:一键部署AI绘画工作流,解决环境配置难题

在AI绘画和生成式AI领域,Stable Diffusion作为开源的扩散模型,为图像生成提供了强大的技术基础。其核心原理是通过学习数据分布,对随机噪声进行逐步去噪,最终生成高质量的图像。这项技术的价值在于极大地降低了专业图像创作的门槛,并推动了AIGC(人工智能生成内容)在艺术设计、游戏开发、广告营销等场景的广泛应用。然而,其典型工作流实现工具ComfyUI,在实际部署中常因Python环境、PyTorch版本与CUDA驱动之间的复杂依赖关系而受阻,尤其是面对NVIDIA 30系、40系及未来50系等不同

优快云头条 675

轻松入门枚举类Enum源码

一、源码解读 public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { /** * 枚举常量的名称 * 使用toString方法访问此字段。 */ private final String name; ...

旅途 664

Blender骨骼绑定实战:如何用IK控制器让角色动作更自然(附常见问题解决)

本文深入探讨了Blender中IK控制器的实战应用,旨在解决角色动作僵硬问题。通过对比IK与FK的核心差异,详细解析了IK链的构建、参数设置及极向目标的使用,并提供了控制器定制、IK/FK无缝切换等高级技巧,帮助动画师实现更自然、符合直觉的角色动画。

weixin_29243279的博客 442

5分钟搞懂基因组规模代谢网络(GSMM):从数据库选择到模型构建全流程

本文详细解析基因组规模代谢网络(GSMM)构建的全流程,从数据库选择到模型优化。重点介绍MetaCyc、BiGG等主流数据库的黄金三角评估法则,对比自上而下与自下而上两种建模方法,并提供模型修剪、仿真优化的实用技巧与避坑指南,帮助研究者高效构建精准代谢网络模型。

weixin_30872157的博客 139

枚举

枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。下面先来看看什么是枚举?如何定义枚举?枚举的定义回忆一下下面的程序,这是在没有枚举类型时定义常量常见的方式/** * Created by zejian on 2017/5/7. * Blog...

Dear_mango的博客 550

VS2022离线安装总闪退?可能是这个--noWeb参数没加对(完整参数指南)

本文深入解析了VS2022离线安装过程中常见的闪退问题,指出核心原因往往在于命令行参数使用不当,特别是`--noWeb`参数。文章提供了完整的参数使用指南和实战解决方案,包括证书处理、安装器文件修复及自动化脚本,帮助用户彻底解决离线部署中的验证失败与闪退难题。

weixin_28842269的博客 761

java枚举类型的实现原理

Java从JDK1.5开始支持枚举,也就是说,Java一开始是不支持枚举的,就像泛型一样,都是JDK1.5才加入的新特性。通常一个特性如果在一开始没有提供,在语言发展后期才添加,会遇到一个问题,就是向后兼容性的问题。像Java在1.5中引入的很多特性,为了向后兼容,编译器会帮我们写的源代码做很多事情,比如泛型为什么会擦除类型,为什么会生成桥接方法,foreach迭代,自动装箱/拆箱等,这有个术语叫

痴人说梦 2万+

从原理图到PCB:基于Allegro的8槽PXIe背板高速设计实战

本文详细介绍了基于Allegro软件进行8槽PXIe背板高速设计的完整实战流程。从理解规范、原理图设计、PCB布局规划,到高速PCIe差分对与精密时钟的布线艺术,再到电源与地处理以及最终的设计验证与生产文件输出,系统性地阐述了确保背板信号完整性、电源完整性和系统稳定性的关键技术与工程实践。

weixin_28312391的博客 209

YOLOv8 交通流量监测详解

本文介绍了一个基于YOLOv8的交通流量监测系统,该系统通过深度学习技术实现车辆实时检测、跟踪和分类。系统核心功能包括:1)使用YOLOv8模型检测道路车辆并进行持续跟踪;2)按方向和车型统计车流量;3)支持多区域流量监测和数据可视化。文章提供了完整的代码实现示例,涵盖基础车辆检测、视频流处理和高级数据分析功能。系统还可进一步优化性能,如使用轻量级模型、GPU加速等,并建议扩展车速估算、密度分析等功能。该方案为交通管理提供了有效的实时监测和决策支持工具。

u010117029的博客 347

JAVA 枚举的基础和原理

当定义一个枚举类型时,每一个枚举类型成员都可以看作是 Enum 类的实例,这些枚举成员默认都被 final、public, static 修饰,当使用枚举类型成员时,直接使用枚举名称调用成员即可。java 为每个枚举都定义了两个属性,name和ordinal,name表示我们定义的枚举常量的名称,如ADD、SUBTRACT等,而ordinal是一个顺序号,根据定义的顺序分别赋予一个整形值,从0开始,因此该枚举对象可以调用name与ordinal方法。你可以将枚举类当成一个普通的java类,为其添加方法。..

sekever的博客 994

FH8626V100之开发环境搭建

FH8626V100是富瀚微出的一款带摄像头的方案,开发工具在Linux 上进行。版本 Ubuntu 20.04 64位。

xue_nuo的博客 1万+

java 枚举_java枚举类型的实现原理

Java从JDK1.5开始支持枚举,也就是说,Java一开始是不支持枚举的,就像泛型一样,都是JDK1.5才加入的新特性。通常一个特性如果在一开始没有提供,在语言发展后期才添加,会遇到一个问题,就是向后兼容性的问题。像Java在1.5中引入的很多特性,为了向后兼容,编译器会帮我们写的源代码做很多事情,比如泛型为什么会擦除类型,为什么会生成桥接方法,foreach迭代,自动装箱/拆箱等,这有个术语叫...

weixin_39846378的博客 93

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

java enum原理_Java枚举的实现原理

Java的枚举类,一般的常用方式是使用枚举代表各类选项,既限定了接受值的范围,又便于记忆。如使用枚举定义星期几、性别等等。看一个最简单的枚举:public enum SimpleEnum {MONDAY, TUESDAY;}在使用这个枚举时,我们可以使用以下的方法:SimpleEnum.TUESDAY.name();SimpleEnum.MONDAY.ordinal();SimpleEnum.va...

weixin_39756192的博客 222

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

java枚举的底层实现原理

http://www.makeru.com.cn/live/1394_203.html?s=144282 01 先定义一个手机操作系统类型枚举PhoneOsEnum: public enum PhoneOsEnum { /** 安卓 */ ANDROID(1, “android”), /** ios */ IOS(2, “ios”); private final Integer type; private final String typeName; PhoneOsEnum(Integer type

weixin_47979914的博客 502

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

java 枚举 单例 原理_java枚举的底层实现原理

01先定义一个手机操作系统类型枚举PhoneOsEnum:public enum PhoneOsEnum {/*** 安卓*/ANDROID(1, "android"),/*** ios*/IOS(2, "ios");private final Integer type;private final String typeName;PhoneOsEnum(Integer type, String t...

weixin_30026297的博客 414

Java枚举的实现_java枚举类型的实现原理

Java从JDK1.5开始支持枚举,也就是说,Java一开始是不支持枚举的,就像泛型一样,都是JDK1.5才加入的新特性。通常一个特性如果在一开始没有提供,在语言发展后期才添加,会遇到一个问题,就是向后兼容性的问题。像Java在1.5中引入的很多特性,为了向后兼容,编译器会帮我们写的源代码做很多事情,比如泛型为什么会擦除类型,为什么会生成桥接方法,foreach迭代,自动装箱/拆箱等,这有个术语叫...

weixin_39711348的博客 210

java 实现枚举_java枚举的实现原理

基本使用首先,所有枚举类型都有一个基类:java.lang.Enum抽象类,里面提供了一些基础属性和基础方法。枚举类型不仅可以定义枚举常量,还可以定义属性、构造方法、普通方法、抽象方法等,比如,我们定义了一个包含成员变量,构造方法以及抽象方法的四则运算操作符枚举类型:public enum Operator {ADD("+") {@Overridepublic int calculate(int ...

weixin_40000702的博客 309

android 枚举_java枚举的底层实现原理

JavaEE:12天掌握Java语言开发 - Java-数据类型 - 创客学院直播室01先定义一个手机操作系统类型枚举PhoneOsEnum:public enum PhoneOsEnum {/** * 安卓 */ANDROID(1, "android"),/** * ios */IOS(2, "ios");private final Integer type;private final Stri...

weixin_39812465的博客 153

Java Enum Trick(Java枚举实现原理)

       Java 从JDK1.5开始引入了枚举特性,用户可以直接采用与C++类似的方式定义枚举类型。有趣的是,Java 枚举的实现是在字节码编译器层次,并未修改JVM底层。本文主要在字节码层面分析Java Enum的实现方式。代码1在enumtestpkg包中定义了MyColor枚举类型,其包含red,blue两个枚举常量。 #代码1package enumtestpkg; public ...

nanguohao的博客 415

深入浅出 Java 中枚举的实现原理

Java 中的枚举— Enum 的全称为 Enumeration, 在 JDK 1.5 时就引入的特性,通过 java.lang包中调用。 基本概述 在 JDK1.5 之前,通过定义常量使用的都是: public static fianl。而枚举的设计,就是把相关的常量分组到一个枚举类型里,方便创建和管理。 比如我们要定义一个颜色常量: public enum Colour { RED, YELLOW, BLUE, GREEN } 这段代码通过底层编译后,实际创建了4个枚举对象:...

欢迎来到我的博客 144

深度解析 Java 枚举类的实现原理

本文深入解析了 Java 枚举类的实现原理和应用场景。枚举本质是继承自 java.lang.Enum 的 final 类,编译后会转换为普通类,枚举常量作为 public static final 字段在静态代码块中初始化。文章详细介绍了枚举的底层实现、核心方法、反射机制,并列举了状态管理、单例模式等典型应用场景。最后提供了常见面试题清单,涵盖枚举原理、应用实践和底层细节,帮助开发者全面理解枚举特性。

weixin_43829708的博客 1025

java 枚举 整形_java枚举类型的实现原理

Java从JDK1.5开始支持枚举,也就是说,Java一开始是不支持枚举的,就像泛型一样,都是JDK1.5才加入的新特性。通常一个特性如果在一开始没有提供,在语言发展后期才添加,会遇到一个问题,就是向后兼容性的问题。像Java在1.5中引入的很多特性,为了向后兼容,编译器会帮我们写的源代码做很多事情,比如泛型为什么会擦除类型,为什么会生成桥接方法,foreach迭代,自动装箱/拆箱等,这有个术语叫...

weixin_34397537的博客 300
上一篇: 遵守普遍接受的命名规范
下一篇: BLE开发资料汇总
我叫王哈哈
博客等级 码龄11年 36粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值