@Override
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Nom est,%s %s.",
this.lastName, this.firstName);
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
}
@WebServlet
urlPatterns = "/init-param"
@Deprecated
since =" 9 "
)Integer.java
/**
*Omission
* @deprecated
* It is rarely appropriate to use this constructor. The static factory
* {@link #valueOf(int)} is generally a better choice, as it is
* likely to yield significantly better space and time performance.
*/
@Deprecated(since="9")
public Integer(int value) {
this.value = value;
}
@SuppressWarnings
public class AnnotationBasic {
public static Integer process() {
@SuppressWarnings("deprecation")
var i = new Integer(108);
return i;
}
public static void main(String[] args) {
System.out.println(process());
}
}
@SafeVarargs
@ ClassInfo (version =" 2.1 ", description =" test d'annotation ")
* @ClassInfo(version="2.1")
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//Déclaration des informations de configuration
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
//@Définition d'annotation avec la commande d'interface
public @interface ClassInfo {
//Déclarer les attributs pouvant être utilisés dans les annotations
String value() default "";
String version() default "";
String description() default "";
}
package mypackage;
//Spécification d'annotation ClassInfo
@ClassInfo(version = "2.1", description = "Test d'opération d'annotation")
// @ClassInfo("2.1")
public class AnnotationClient {
public static void main(String[] args) throws ClassNotFoundException {
//Obtenir des membres subordonnés via l'objet de classe
var clazz = Class.forName("mypackage.AnnotationClient");
//Obtenir les annotations données à la classe par la méthode getAnnotation
var info = (ClassInfo)
clazz.getAnnotation(ClassInfo.class);
System.out.println("version:" +
(info.value().equals("") ? info.version() : info.value()));
System.out.println("Explication:" + info.description());
}
}
import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
public class ReflectInstance {
public static void main(String[] args) {
try {
//Obtenir la classe File
var clazz = File.class;
//Créer un objet File via le constructeur
var c = clazz.getConstructor(String.class);
var fl = c.newInstance("./data/data.txt");
System.out.println(fl);
//Génération de baies de taille 2
var list = (File[]) Array.newInstance(File.class, 2);
Array.set(list, 0, fl);
System.out.println(Arrays.toString(list));
} catch (InstantiationException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
import java.io.File;
public class ReflectMethods {
public static void main(String[] args) {
//Obtenir la classe File
var str = File.class;
//Répertoriez les méthodes publiques sous la classe File avec getName
for (var m : str.getMethods()) {
System.out.println(m.getName());
}
}
}
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectInvoke {
public static void main(String[] args) {
try {
//Obtenir la classe File
var clazz = File.class;
//Création d'objet fichier
var f1 = clazz.getConstructor(String.class).newInstance("./data/data.txt");
var f2 = clazz.getConstructor(String.class).newInstance("./data/sample.txt");
//Récupérer et exécuter la méthode renameTo
Method m = clazz.getMethod("renameTo", File.class);
System.out.println(m.invoke(f1, f2));
} catch (NoSuchMethodException | SecurityException |
InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
//Obtient le champ lastName de la classe Person
import java.lang.reflect.InvocationTargetException;
import example.Person;
public class ReflectField {
public static void main(String[] args) {
try {
var clazz = Person.class;
var con = clazz.getConstructor(String.class, String.class);
var p = con.newInstance("Taro", "Yamada");
//Acquisition sur le terrain
var last = clazz.getDeclaredField("lastName");
//Autoriser explicitement l'accès aux champs privés
last.setAccessible(true);
//get/Obtenir la valeur du champ avec la méthode set/Configuration
last.set(p, "Suzuki");
System.out.println(last.get(p)); //Suzuki
} catch (NoSuchMethodException | SecurityException |
InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException |
NoSuchFieldException e) {
e.printStackTrace();
}
}
}
package example;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Nom est,%s %s.",
this.lastName, this.firstName);
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
}
Recommended Posts