问题描述
请告诉我Java中C# Type.GetType()
的语法是否等效.
Please let me know Is there any equivalent syntax for C# Type.GetType()
in java.
以及Java中 Activator.CreateInstance()
的等效语法.
And equivalent syntax of Activator.CreateInstance()
in java.
谢谢.
推荐答案
等同于Type.GetType()
Type.GetType() equivalent
使用对象.getClass()或 instanceof
Activator.CreateInstance()等效项
Activator.CreateInstance() equivalent
如果类具有无参数构造函数,则通常使用 Class.forName 和 Class.newInstance 如下
If a class has a no-argument constructor, then creating an object from its package-qualified class name is usually done using using Class.forName and Class.newInstance like below
Class clazz = Class.forName("test.Demo");
Demo demo = (Demo) clazz.newInstance();
如果类本体具有无参数构造函数,则必须使用如下所示的Reflection
If class dosent have no-argument constructor you must use Reflection like below
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
这篇关于Java中C#Type.GetType()是否有任何等效语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!