Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How is the enumeration of Java and C++ different from reflection?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the difference between enumeration and reflection of Java and C++". In daily operation, I believe many people have doubts about the enumeration and reflection of Java and C++. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the difference between enumeration and reflection of Java and C++". Next, please follow the editor to study!

1. Enumeration:

Enumeration is supported in Java 1.5SE. Here are the basic concepts and application techniques of Java enumeration:

1. All enum objects are supported by the class object as the underlying support, which inherits from Enum in JDK, but the underlying class is really a final class and can no longer be inherited by other classes.

two。 The emergence of enumerations completely replaces the original "public static final" constant representation, and enumerations replace the original scheme in a more reasonable, elegant, and secure way. The most basic form of declaration is as follows:

Public enum Color {RED, BLUE, BLACK, YELLOW}

3. The prototype of the constructor in Enum is protected Enum (String name, int ordinal). The custom enumeration object will put its name in the form of a string, and at the same time pass itself to the superclass as two parameters of the superclass constructor from the declared order of the constant, and the superclass will complete the necessary initialization. For example, the RED enumeration constant will call super ("RED", 0).

4. Constructors, domain methods, and domain fields can be defined in enumerations, but the constructors in enumerations must be private (private). If there is a custom constructor in the custom enumeration, each enumeration constant must be declared according to the rules of the custom constructor. The constructor of an enumerated object is called only once when the enumerated constant object is declared, and can no longer be created through the new method like a normal object, as shown in the following code:

Public enum Size {SMALL, MEDIUM, LARGE; double pricingFactor; private Size (double p) {pricingFactor = p;}}

Note: the enumeration constant list must be written at the top of the declaration, otherwise the compiler reports an error.

5. You can add domain methods to custom enumerations, as shown in the following code:

Public enum Size {SMALL, MEDIUM, LARGE; private double pricingFactor; Size (double p) {pricingFactor = p;} public double getPricingFactor () {return pricingFactor;}}

6. Common domain methods in enumerations:

Public enum Size {SMALL, MEDIUM, LARGE;} public static void main (String [] args) {/ / two methods to get enumerated types Size S1 = Size.SMALL; / / valueOf the function prototype is T valueOf (Class enumType,String name) Size S2 = Enum.valueOf (Size.class, "SMALL"); / / the valueOf method of Size (custom enumeration) is automatically inserted by the Java compiler when it generates bytecode. Size S3 = Size.valueOf ("MEDIUM"); / / 1 / / the result is the same as above, the enumeration overloads the equals method System.out.println ("Size.MEDIUM.equals (Enum.valueOf (Size.class,\" MEDIUM\ ")):" + Size.MEDIUM.equals (Enum.valueOf (Size.class, "MEDIUM") / / iterate through all members of the enumerated type. The Size.values method applied here, like the Size.valueOf method / /, is automatically inserted by the compiler when the bytecode is generated. Both the for (Size s:Size.values ()) {/ / 2 / / ordinal () and name () methods are methods provided by Enum, which return the string name passed in the enumeration constant when the superclass constructor is called automatically in the constructor / constructor at the time of declaration, and the sequence number System.out.println (s.ordinal () + "+ s.name () +" + s.toString ()) in the declaration list. By default, the} / / compareTo method compares the return value of ordinal () of the enumeration constant. If (s1.compareTo (S3) < 0) System.out.println ("Size.SMALL is less than Size.MEDIUM");}

7. Class bodies based on specific constants can be declared in the enumeration, as shown in the following code:

Public enum Size {/ / Small, ExtraLarge, and ExtraExtraLarge all use custom getPricingFactor / / methods to override the default getPricingFactor methods provided by Size. Small {@ Override public double getPricingFactor () {return 0.8;}}, / / Medium and Large will use the getPricingFactor method implemented by default inside Size. Medium, Large, ExtraLarge {@ Override public double getPricingFactor () {return 1.2;}}, ExtraExtraLarge {@ Override public double getPricingFactor () {return 1.2;}}; public double getPricingFactor () {return 1.0;}} public static void main (String args []) {for (Size s: Size.values ()) {double d = s.getPricingFactor (); System.out.println (s + "Size has pricing factor of" + d) }} / * the result is as follows: Small Size has pricing factor of 0.8 Medium Size has pricing factor of 1.0 Large Size has pricing factor of 1.0 ExtraLarge Size has pricing factor of 1.2 ExtraExtraLarge Size has pricing factor of 1.2 * /

8. For the use of enumerations in the switch statement, see the following code:

Public enum Color {RED, BLUE, BLACK, YELLOW} public static void main (String [] args) {Color m = Color.BLUE; / / case statement references the enumeration constant without adding the enumerated type name. Switch (m) {case RED: System.out.println ("color is red"); break; case BLACK: System.out.println ("color is black"); break; case YELLOW: System.out.println ("color is yellow"); break; case BLUE: System.out.println ("color is blue"); break; default: System.out.println ("color is unknown"); break;}}

9. The two containers EnumMap and EnumSet related to enumerations are declared and mainly used as follows.

EnumMap EnumSet / / Code Example 1 public enum State {ON, OFF}; public static void main (String [] args) {/ / EnumSet EnumSet stateSet = EnumSet.allOf (State.class); for (State s: stateSet) System.out.println (s); / / EnumMap use EnumMap stateMap = new EnumMap (State.class); stateMap.put (State.ON, "is On"); stateMap.put (State.OFF, "is off") For (State s: State.values ()) System.out.println (s.name () + ":" + stateMap.get (s));} / / Code Example 2 public enum Size {Small,Medium,Large} public static void main (String args []) {Map map = new EnumMap (Size.class); map.put (Size.Small, 0.8); map.put (Size.Medium, 1.0); map.put (Size.Large, 1.2) For (Map.Entry entry: map.entrySet ()) helper (entry);} private static void helper (Map.Entry entry) {System.out.println ("Map entry:" + entry);}

10. There are two main differences between Java enumeration and C++ enumeration. One is that constants can only be defined in C++ enumerations, which are mainly used in switch clauses. The other is that enumeration constants in C++ can directly perform various mathematical operations with numeric variables.

Second, reflection:

1. The reflection mechanism of Java is mainly shown in four points:

1) the ability to analyze classes in operation

2) View objects while running

3) implement the operation code of the array

4) use the Method object, which is very similar to the function pointer in C++.

Note: the reflection-based application of Java is mainly used in the development of some tool class libraries, but there are few scenarios in the actual application development.

two。 Get the name of the object (as a string) vs creates an instance of the object by its name (as a string), as shown in the following code:

Public static void main (String args []) {/ / 1. Get the name Date d = new Date (); / / or Class represented by the string through the object

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report