zoukankan      html  css  js  c++  java
  • JAVA学习--反射其他操作

    1 //1.获取运行时类的父类
    2     @Test
    3     public void test1(){
    4         Class clazz = Person.class;
    5         Class superClass = clazz.getSuperclass();
    6         System.out.println(superClass);
    7     }
    1     //2.获取带泛型的父类
    2     @Test
    3     public void test2(){
    4         Class clazz = Person.class;
    5         Type type1 = clazz.getGenericSuperclass();
    6         System.out.println(type1);
    7     }
     1     //3*.获取父类的泛型
     2     @Test
     3     public void test3(){
     4         Class clazz = Person.class;
     5         Type type1 = clazz.getGenericSuperclass();
     6         
     7         ParameterizedType param = (ParameterizedType)type1;
     8         Type[] ars = param.getActualTypeArguments();
     9         
    10         System.out.println(((Class)ars[0]).getName());
    11     }
    12     
    1     //4.获取实现的接口
    2     @Test
    3     public void test4(){
    4         Class clazz = Person.class;
    5         Class[] interfaces = clazz.getInterfaces();
    6         for(Class i : interfaces){
    7             System.out.println(i);
    8         }
    9     }
    1     //5.获取所在的包
    2     @Test
    3     public void test5(){
    4         Class clazz = Person.class;
    5         Package pack = clazz.getPackage();
    6         System.out.println(pack);
    7     }
    1     //6.获取注解
    2     @Test
    3     public void test6(){
    4         Class clazz = Person.class;
    5         Annotation[] anns = clazz.getAnnotations();
    6         for(Annotation a : anns){
    7             System.out.println(a);
    8         }
    9     }
     1 @MyAnnotation(value = "atguigu")
     2 public class Person extends Creature<String> implements Comparable,MyInterface{
     3     public String name;
     4     private int age;
     5     int id;
     6     //创建类时,尽量保留一个空参的构造器。
     7     public Person() {
     8         super();
     9 //        System.out.println("今天天气很闷热");
    10     }
    11     public Person(String name) {
    12         super();
    13         this.name = name;
    14     }
    15     private Person(String name, int age) {
    16         super();
    17         this.name = name;
    18         this.age = age;
    19     }
    20     public String getName() {
    21         return name;
    22     }
    23     public void setName(String name) {
    24         this.name = name;
    25     }
    26     public int getAge() {
    27         return age;
    28     }
    29     public void setAge(int age) {
    30         this.age = age;
    31     }
    32     
    33     public int getId() {
    34         return id;
    35     }
    36     public void setId(int id) {
    37         this.id = id;
    38     }
    39     @MyAnnotation(value = "abc123")
    40     public void show(){
    41         System.out.println("我是一个人!");
    42     }
    43     
    44     private Integer display(String nation,Integer i) throws Exception{
    45         System.out.println("我的国籍是:" + nation);
    46         return i;
    47     }
    48     @Override
    49     public String toString() {
    50         return "Person [name=" + name + ", age=" + age + "]";
    51     }
    52     @Override
    53     public int compareTo(Object o) {
    54         // TODO Auto-generated method stub
    55         return 0;
    56     }
    57     
    58     public static void info(){
    59         System.out.println("中国人!");
    60     }
    61     
    62     class Bird{
    63         
    64     }
     1 import static java.lang.annotation.ElementType.CONSTRUCTOR;
     2 import static java.lang.annotation.ElementType.FIELD;
     3 import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
     4 import static java.lang.annotation.ElementType.METHOD;
     5 import static java.lang.annotation.ElementType.PARAMETER;
     6 import static java.lang.annotation.ElementType.TYPE;
     7 
     8 import java.lang.annotation.Retention;
     9 import java.lang.annotation.RetentionPolicy;
    10 import java.lang.annotation.Target;
    11 
    12 @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    13 @Retention(RetentionPolicy.RUNTIME)
    14 public @interface MyAnnotation {
    15     String value();
    16 }
    1 public class Creature<T>{
    2     public double weight;
    3     
    4     public void breath(){
    5         System.out.println("呼吸!");
    6     }
    7 }

     1 import java.io.Serializable; 2 3 public interface MyInterface extends Serializable{ 4 5 } 

  • 相关阅读:
    poj 2002 Squares 几何二分 || 哈希
    hdu 1969 Pie
    hdu 4004 The Frog's Games 二分
    hdu 4190 Distributing Ballot Boxes 二分
    hdu 2141 Can you find it? 二分
    Codeforces Round #259 (Div. 2)
    并查集
    bfs
    二维树状数组
    一维树状数组
  • 原文地址:https://www.cnblogs.com/zhangfan94/p/4273234.html
Copyright © 2011-2022 走看看