zoukankan      html  css  js  c++  java
  • 利用java.lang.reflect.Constructor动态实例化对象

     1 public class Student {
     2     private String name;
     3     private Integer age;
     4     private Student(String name,Integer age){
     5         this.name=name;
     6         this.age=age;
     7     }
     8     public String getName() {
     9         return name;
    10     }
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14     public Integer getAge() {
    15         return age;
    16     }
    17     public void setAge(Integer age) {
    18         this.age = age;
    19     }
    20 }
     1 package com.mingrisoft;
     2 
     3 import java.lang.reflect.Constructor;
     4 
     5 public class NewClassTest {
     6     public static void main(String[] args) {
     7         try {
     8             //获得File类的Constructor对象
     9             Constructor<Student> constructor = Student.class.getDeclaredConstructor(new Class[]{String.class,Integer.class});
    10             Student t = constructor.newInstance("libing",30);
    11             System.out.println(t.getName());
    12         } catch (Exception e) {
    13             e.printStackTrace();
    14         }
    15     }
    16 }
    坚持,坚持,再坚持。
  • 相关阅读:
    Git远程操作
    696. Count Binary Substrings
    693. Binary Number with Alternating Bits
    821. Shortest Distance to a Character
    345. Reverse Vowels of a String
    89. Gray Code
    数组操作符重载
    C++字符串反转
    马克思的两面性-来自网友
    C++字符串
  • 原文地址:https://www.cnblogs.com/walk-the-Line/p/4845126.html
Copyright © 2011-2022 走看看