Records represent a simplified form of classes whose methods are implicitly derived from the attributes defined as constructor parameters. Public Book(String.In this blog, I will present Records, one of my favorite features of modern Java. The final code for the Book class is as follows: In this class, declare a String variable to represent the book name, two int variables to represent the book number and price, and provide three construction methods. The following is a case to demonstrate how to call the method of the Constructor class to obtain the information of the constructor.ġ) First create a Book class to represent book information. String allModifiers = Modifier.toString(modifiers) Int modifiers = con.getModifiers() // get the modifier integer of the constructorīoolean isPublic = Modifier.isPublic(modifiers) // Determine if the modifier integer is public Returns true if the final modifier is used, otherwise returns falseįor example, the following code determines whether the constructor represented by the object con is modified by public, and obtains all the modifiers of the constructor as a string. Returns true if modified with the private modifier, false otherwise Returns true if modified with the protected modifier, false otherwise Returns true if the public modifier is used, otherwise returns false Returns true if the static modifier is used, otherwise returns false Table 2 lists the commonly used static methods of the Modifier class. A series of static methods for parsing are provided in this class, not only to check whether it is modified by the specified modifier, but also to obtain all modifiers in the form of strings. The modifier information represented by the return value of the getMocMers() method can be parsed through the class. Obtains an integer from which the modifiers used by this constructor can be parsed If the method is executed first, and the entry parameter is set to true, object creation is allowed If the permission of the constructor is private, by default it is not allowed to use the netlnstance() method to create objects through reflection. If no parameters are set, it means that the default no-parameter constructor is used. Use this constructor to create an object of this type with the specified parameters. Get the types of exceptions that the constructor may throw as an array of Classes Get the type of each parameter of the constructor in the form of an array of Classes in the order of declaration Table 1 Common methods of Constructor classĬheck if the constructor allows a variable number of parameters, if so, return true, otherwise return false Common methods of the Constructor class are shown in Table 1. ObjectClass.getDeclaredConstructor(new Class) Įach Constructor object created represents a constructor, which is then manipulated using the methods of the Constructor object. ObjectClass.getDeclaredConstructor(int.class,String.class) For example, to access a constructor whose entry parameter types are int and String in turn, the following two methods can be implemented. If it is to access the specified constructor, it needs to be accessed according to the type of the entry parameter of the constructor. GetDeclaredConstructor(Class.parameterTypes) In order to dynamically obtain the information of the object construction method, you first need to create an object or array ofĬonstructor type through one of the following methods. Java Use Reflection In Remote Method Invocation.Java Access Member Variables Through Reflection.Java Executes A Method Through Reflection.Java Access Constructor Through Reflection.Java Manipulate Collection with Predicate.Java Traverse A Collection with foreach.Java Traverse A Collection with Iterator.Java Authenticate Username And Password.Java 9 Enhanced Automatic Resource Management.Java Wrapper Classes, Boxing And Unboxing.Java String StringBuffer and StringBuilder Differences.Java Selection/Looping Structures Summary.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |