http://stackoverflow.com/questions/1268817/create-new-class-from-a-variable-in-java
Consider I have a class:
com.crossedstreams.thingy.Foo
com.crossedstreams.thingy.Foo
Which has a constructor with signature:
Foo(String a, String b);
I would instantiate the class based on these two facts as follows:
// Load the Class. Must use fully qualified name here!
Class clazz = Class.forName("com.crossedstreams.thingy.Foo");
// I need an array as follows to describe the signature
Class[] parameters = new Class[] {String.class, String.class};
// Now I can get a reference to the right constructor
Constructor constructor = clazz.getConstructor(parameters);
// And I can use that Constructor to instantiate the class
Object o = constructor.newInstance(new Object[] {"one", "two"});
// To prove it's really there...
System.out.println(o);
Output:
com.crossedstreams.thingy.Foo@20cf2c80
There's plenty of resources out there which go into more detail about this, and you should be aware that you're introducing a dependency that the compiler can't check for you - if you misspell the class name or anything, it will fail at runtime. Also, there's quite a few different types of Exception that might be throws during this process. It's a very powerful technique though.
分享到:
相关推荐
·调用实例工厂方法创建bean 时,必须使用factory-bean 属性来确定工厂bean。 而静态工厂方法则使用class 元素确定静态工厂类。 其相同之处如下: ·都需使用factory-method属性指定产生bean 实例的工厂方法。 ...
实例070 实例化Class类的几种方式 107 实例071 查看类的声明 108 实例072 查看类的成员 110 实例073 查看内部类信息 112 实例074 动态设置类的私有域 113 实例075 动态调用类中方法 115 实例076 动态实例化类 116 ...
请生成相应的getter、setter方法,并创建两个构造方法:Person(),Person(String idNo),把Person编译成.class文件。请通过反射技术,抽取Person所有的属性、方法,重新拼接成源文件字符串,再将字符串写入到一个新...
// 创建实例;调用方法(传递参数,获取返回值) object instance = assembly.CreateInstance(type.ToString()); string rt = (string)method.Invoke(instance, parameters); // 打印输出 Console.WriteLine(rt)...
UseParaThread 演示带参数线程的使用实例 UseLock 演示使用lock语句同步数据实例 UseMonitor 演示使用Monitor类同步数据实例 UseMutex 演示使用Mutex类同步数据实例 第13章(\ch13) 示例描述:介绍动态链接库...
class是引用类型,创建一个class类型实例被分配在托管堆上。但struct和class的区别远不止这么简单。 概括来讲,struct和class的不同体现在: ● 类是引用类型,struct是值类型 ● 在托管堆上创建类的实例,在栈上...
3.在类的方法中(如__init__),第一参数永远是self,表示创建的类实例本身,而不是类本身。 4.可以把对象的各种属性绑定到self。 5.self代表当前对象的地址。self能避免非限定调用造成的全局变量。 6.self不是...
Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...
在空场景中创建一个空节点名字为unityObject,注意:这个名字是等会再Android中向Unity发送消息时的API中必须要传的参数 然后在节点上添加一个脚本,脚本中的代码如下: using UnityEngine; using UnityEngine.UI; ...
//使用方法实例创建表达类型 ExpressionStatement statement2 = ast.newExpressionStatement(methodInvocation2); //将表达式添加到语句块 mainBlock.statements().add(statement2); } //构造方法 { //...
当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("<span/>") 或 $("<span></span>") ,但不推荐 $("<span>") --------------------------------------------------------------...
Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...
实例使用了加载类似xml文件读取信息,然后显示。 COM_ATL_Tutorial 简单的atl控件演示 COM接口挂钩及其应用 如题。 CSkinSlier CSliderCtrl自绘 Cursor 生成图标,运行到鼠标图标。 cutscene win32下实现视频...
JLPCActor 如果你要定义一个Actor类,那么你需要让它继承自JLPCActor这个类,你所定义的这个类的方法不必是线程安全的,它们通常会在...getAncestor(actorClass)方法返回注解给它所实现接口的类的实例或者空对象。
def 函数名(参数列表): 语句块 实例方法 method def 实例方法名(self, 形参1, 形参2, ...) # 调用语句 对象.实例方法名(实参1, 实参2) 属性: 对象.属性名 = 表达式 v = 对象.属性名 实始化方法 def __...
这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句 <?php /* ******************************************************************* Example file This example shows how to ...
定义:constructor()方法是类的构造函数(默认方法),作用是传递参数,返回实例对象,通过new命令生成实例对象时,自动调用此方法,如果没有显示定义,类内部会自动创建一个constructor() 例: 接着上一个定义的 ...