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  

  • 相关阅读:
    Centos6.7 编译安装 MySQL教程
    python os os.path模块学习笔记
    Ubuntu无线转有线教程
    k8s 部署kube-dns
    k8s-应用快速入门(ma)
    kubectl工具管理应用生命周期
    k8s-部署WEB-UI(dashboard)
    k8s-集群状态及部署一个实例
    k8s-创建node节点kubeconfig配置文件
    k8s-flannel容器集群网络部署
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1815913.html
Copyright © 2011-2022 走看看