zoukankan      html  css  js  c++  java
  • JAVA学习--反射构造器操作

     1 @Test
     2     public void test1() throws Exception{
     3         String className = "com.atguigu.java.Person";
     4         Class clazz = Class.forName(className);
     5         //创建对应的运行时类的对象。使用newInstance(),实际上就是调用了运行时类的空参的构造器。
     6         //要想能够创建成功:①要求对应的运行时类要有空参的构造器。②构造器的权限修饰符的权限要足够。
     7         Object obj = clazz.newInstance();
     8         Person p = (Person)obj;
     9         System.out.println(p);
    10     }
     1 @Test
     2     public void test2() throws ClassNotFoundException{
     3         String className = "com.atguigu.java.Person";
     4         Class clazz = Class.forName(className);
     5         
     6         Constructor[] cons = clazz.getDeclaredConstructors();
     7         for(Constructor c : cons){
     8             System.out.println(c);
     9         }
    10     }
     1 //调用指定的构造器,创建运行时类的对象
     2     @Test
     3     public void test3() throws Exception{
     4         String className = "com.atguigu.java.Person";
     5         Class clazz = Class.forName(className);
     6         
     7         Constructor cons = clazz.getDeclaredConstructor(String.class,int.class);
     8         cons.setAccessible(true);
     9         Person p = (Person)cons.newInstance("罗伟",20);
    10         System.out.println(p);
    11     }
     1  class Person extends Creature<String> implements Comparable,MyInterface{
     2     public String name;
     3     private int age;
     4     int id;
     5     //创建类时,尽量保留一个空参的构造器。
     6     public Person() {
     7         super();
     8 //        System.out.println("今天天气很闷热");
     9     }
    10     public Person(String name) {
    11         super();
    12         this.name = name;
    13     }
    14     private Person(String name, int age) {
    15         super();
    16         this.name = name;
    17         this.age = age;
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public int getAge() {
    26         return age;
    27     }
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31     
    32     public int getId() {
    33         return id;
    34     }
    35     public void setId(int id) {
    36         this.id = id;
    37     }
    38     @MyAnnotation(value = "abc123")
    39     public void show(){
    40         System.out.println("我是一个人!");
    41     }
    42     
    43     private Integer display(String nation,Integer i) throws Exception{
    44         System.out.println("我的国籍是:" + nation);
    45         return i;
    46     }
    47     @Override
    48     public String toString() {
    49         return "Person [name=" + name + ", age=" + age + "]";
    50     }
    51     @Override
    52     public int compareTo(Object o) {
    53         // TODO Auto-generated method stub
    54         return 0;
    55     }
    56     
    57     public static void info(){
    58         System.out.println("中国人!");
    59     }
    60     
    61     class Bird{
    62         
    63     }
    64     
    65 }
     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 } 

  • 相关阅读:
    第二次作业
    第一次作业
    第三次作业
    第二次作业
    第一次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/zhangfan94/p/4273229.html
Copyright © 2011-2022 走看看