【问题标题】:Instantiate a class object with constructor that accepts a string parameter?用接受字符串参数的构造函数实例化一个类对象?
【发布时间】:2011-04-04 04:42:19
【问题描述】:

我想从它的Class 对象实例化一个对象,使用接受单个String 参数的构造函数。

这是一些接近我想要的代码:

Object object = null;
Class classDefinition = Class.forName("javax.swing.JLabel");
object = classDefinition.newInstance();

但是,它实例化了没有文本的JLabel 对象。我想使用接受字符串作为初始文本的JLabel 构造函数。有没有办法从 Class 对象中选择特定的构造函数?

【问题讨论】:

    标签: java html reflection constructor


    【解决方案1】:

    以下内容对您有用。 试试这个,

    Class[] type = { String.class };
    Class classDefinition = Class.forName("javax.swing.JLabel"); 
    Constructor cons = classDefinition .getConstructor(type);
    Object[] obj = { "JLabel"};
    return cons.newInstance(obj);
    

    【讨论】:

      【解决方案2】:

      Class.newInstance 调用无参数构造函数(不带任何参数的构造函数)。为了调用不同的构造函数,您需要使用反射包(java.lang.reflect)。

      像这样获取Constructor 实例:

      Class<?> cl = Class.forName("javax.swing.JLabel");
      Constructor<?> cons = cl.getConstructor(String.class);
      

      getConstructor 的调用指定您需要采用单个String 参数的构造函数。现在创建一个实例:

      Object o = cons.newInstance("JLabel");
      

      你已经完成了。

      附:仅将反射用作最后的手段!

      【讨论】:

      • “作为最后的手段”。实际上存在问题,它是第一个也是唯一的选择,所以不确定你为什么决定这样限定你的帖子。
      【解决方案3】:

      Class.forName("className").newInstance() 总是调用无参数的默认构造函数。

      调用参数化构造函数而不是零参数无参数构造函数,

      1. 您必须通过在Class[] 中传递类型来获取带有参数类型的Constructor 对于ClassgetDeclaredConstructor 方法
      2. 您必须通过在Object[] for
        newInstanceConstructor 方法中传递值来创建构造函数实例

      示例代码:

      import java.lang.reflect.*;
      
      class NewInstanceWithReflection{
          public NewInstanceWithReflection(){
              System.out.println("Default constructor");
          }
          public NewInstanceWithReflection( String a){
              System.out.println("Constructor :String => "+a);
          }
          public static void main(String args[]) throws Exception {
      
              NewInstanceWithReflection object = (NewInstanceWithReflection)Class.forName("NewInstanceWithReflection").newInstance();
              Constructor constructor = NewInstanceWithReflection.class.getDeclaredConstructor( new Class[] {String.class});
              NewInstanceWithReflection object1 = (NewInstanceWithReflection)constructor.newInstance(new Object[]{"StackOverFlow"});
      
          }
      }
      

      输出:

      java NewInstanceWithReflection
      Default constructor
      Constructor :String => StackOverFlow
      

      【讨论】:

        【解决方案4】:

        有时不需要为类创建对象来调用构造函数和方法。您可以在不创建直接对象的情况下调用类的方法。带参数调用构造函数非常简单。

        import java.lang.reflect.*;
        import java.util.*;
        
        class RunDemo
        {
            public RunDemo(String s)
            {
                System.out.println("Hello, I'm a constructor. Welcome, "+s);
            }  
            static void show()
            {
                System.out.println("Hello.");
            }
        }
        class Democlass
        {
            public static void main(String args[])throws Exception
            {
                Class.forName("RunDemo");
                Constructor c = RunDemo.class.getConstructor(String.class);  
                RunDemo d = (RunDemo)c.newInstance("User");
                d.show();
            }
        }
        

        输出将是:

        你好,我是建设者。欢迎,用户

        你好。

        Class.forName("RunDemo"); 将加载 RunDemo 类。

        Constructor c=RunDemo.class.getConstructor(String.class); Constructor 类的 getConstructor() 方法将返回以 String 作为参数的构造函数,其引用存储在对象“c”中构造函数类。

        RunDemo d=(RunDemo)c.newInstance("User"); Constructor 类的方法 newInstance() 将实例化 RundDemo 类并返回对象的 Generic 版本并转换为使用类型转换运行Demo 类型。

        RunDemo 的对象 'd' 持有 newInstance() 方法返回的引用。

        【讨论】:

          猜你喜欢
          • 2014-04-22
          • 1970-01-01
          • 1970-01-01
          • 2016-09-03
          • 1970-01-01
          • 2013-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多