TypeScript enums (hyper-verbose)

The Enum type is used in scenarios where the values are limited to a certain range, for example, there can only be seven days in a week, and the colors are limited to red, green, and blue, etc. TS enumeration is inspired by C# Simple example Enumerations are defined using the enum keyword: enum Days […]

Rust3 Using Structs to Structure Related Data & Enums and Pattern Matching

Rust study notes Rust Programming Language Introduction Tutorial Course Notes Reference textbook: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community) Lecture 5: Using Structs to Structure Related Data //define a struct #[derive(Debug)] struct User{<!– –> username: String, email: String, sign_in_count: u64, active: bool, // trailing comma is […]

Typescript enums and scope reduction

About enumeration Reverse mapping principle In the past, I only knew how to build and use enumerations, but I didn’t know how to get the enumeration content through Typescript. Through keyof typeof Reverse mapping to obtain enumeration key enum Enum { A, } let a = Enum.A; let nameOfA = Enum[a]; // “A” About constant […]

How does switch match enums and strings?

1. In Java, which one is more efficient, if or switch? Conclusion: switch is faster on average public class Animal { ? ? ? } ? ? class Dog extends Animal{ ? } class Cat extends Animal{ ? } ? class Pig extends Animal{ ? } ? ? ? public static Animal test1(int n) { […]

[A Deep Dive into Rust: Structs, Enums, and Pattern Matching]

Directory title Chapter 1: Introduction to Rust: Structs, Enums, and Pattern Matching Why Rust? What Will We Cover? Why These Topics? Chapter 2: Structs: The Building Blocks in Rust 2.1 Defining Structs (defining structures) 2.2 Instantiating Structs (structure instantiation) 2.3 Methods and Associated Functions (structural methods and associated functions) 2.4 Tuple Structs (tuple structure) Chapter […]

Enumeration (EnumSet and EnumMap, etc.)

Article directory 1. Overview 2. Custom enumeration method 3. Use == to compare enumeration types 4. Use enumerated types in switch statements 5. Properties, methods and constructors of enumerated types 6. EnumSet and EnumMap 6.1. EnumSets 6.2. EnumMap 7. Implement some design patterns through enumeration 7.1 Singleton pattern 7.2 Strategy pattern 8. Java 8 and […]

Application of inheritance in enumeration class (Extending Enums in Java)

Extending an Enum Type public enum BasicStringOperation {<!– –> TRIM(“Removing leading and trailing spaces.”), TO_UPPER(“Changing all characters into upper case.”), REVERSE(“Reversing the given string.”); private String description; // constructor and getter } public enum ExtendedStringOperation extends BasicStringOperation {<!– –> MD5_ENCODE(“Encoding the given string using the MD5 algorithm.”), BASE64_ENCODE(“Encoding the given string using the BASE64 algorithm.”); […]

Enumeration classes use Jackson for serialization and deserialization (How To Serialize and Deserialize Enums with Jackson)

Let’s define the following Enum: public enum Distance {<!– –> KILOMETER(“km”, 1000), MILE(“miles”, 1609.34), METER(“meters”, 1), INCH(“inches”, 0.0254), CENTIMETER(“cm”, 0.01), MILLIMETER(“mm”, 0.001); private String unit; private final double meters; private Distance(String unit, double meters) {<!– –> this.unit = unit; this.meters = meters; } // standard getters and setters } Serializing Enums to JSON Default Enum […]

Java Language / Annotations, Enums, Generics, etc. (Assignment)

1. What rules can be followed in the definition of static methods? Summarize in a few words (except for static methods, others are also summarized). Define an enumeration, according to the requirements of the summary. Static method: Static methods must be defined inside the enumeration class, not in an instance of the enumeration. Static methods […]

Custom Types: Structs, Enums, and Unions

Custom types: structures, enumerations and unions structure declaration of the structure self-referencing structure Definition and initialization of structure variables Structure memory alignment Why memory alignment exists Modify the default alignment Struct parameter passing bit segment what is bit segment bit segment memory allocation Cross-platform issues with bit segments enumerate enum type definition Advantages of Enums […]