首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

java取得实例对象的几种方法

2012-07-08 
java获得实例对象的几种方法// 1.new?Dog d1 new Dog()?d1.say()?// 2.Class.forName?Dog d2 (Dog)

java获得实例对象的几种方法

// 1.new?
Dog d1 = new Dog();?
d1.say();?
// 2.Class.forName?
Dog d2 = (Dog) Class.forName("com.test.Dog").newInstance();?
d2.say();?
// 3.ClassLoader?
ClassLoader c = ClassLoader.getSystemClassLoader();?
Class classT = c.loadClass("com.test.Dog");?
Dog d = (Dog) classT.newInstance();?
d.say();?
// 4.Constructor

Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
String str2 = (String)constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
System.out.println(str2.charAt(2));

热点排行