zoukankan      html  css  js  c++  java
  • reflection

     1 import java.lang.reflect.Constructor;
     2 public class TestList {
     3  public static void main(String args[]) throws Exception {
     4   Class c = Class.forName("com.**.MyClass");
     5   //getDeclaredConstructors() reflecting all the constructors declared by the class
     6   //getConstructors() just reflect the public constructors
     7   Constructor constructors[] = c.getDeclaredConstructors();
     8      Object obj = null;
     9      for (Constructor cons : constructors) {
    10        Class[] params = cons.getParameterTypes();
    11        if (params.length == 1 && params[0== int.class) {
    12          obj = cons.newInstance(10);
    13          break;
    14        }
    15        
    16      }
    17      if (obj == null) {
    18        System.out.println("Can’t Create MyClass object.");
    19        return;
    20      }
    21  }
    22 }
    23  
    24  
    25  
    26 public class MyClass {
    27  private int count;
    28  MyClass(int c) {
    29   System.out.println("MyClass(int):" + c);
    30   count = c;
    31  }
    32  MyClass() {
    33   System.out.println("MyClass()");
    34   count = 0;
    35  }
    36  void setCount(int c) {
    37   System.out.println("setCount(int): " + c);
    38   count = c;
    39  }
    40  int getCount() {
    41   System.out.println("getCount():" + count);
    42   return count;
    43  }
    44  void showcount() {
    45   System.out.println("count is " + count);
    46  }
    47 }
    48  

  • 相关阅读:
    [HAOI2009] 毛毛虫
    [NOI2015]品酒大会
    SDOI2016 生成魔咒
    [POJ2406]字符串的幂
    [SPOJ705]不同的子串
    快速幂和矩阵快速幂
    对于最近的一些日常总结by520(17.10.18)
    思维训练
    趣味性的高智商(贼有意思)的题(坑)
    C++手动开O2优化
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1815913.html
Copyright © 2011-2022 走看看