ITeye博客

java创建对象的两种方法-写给自己的博客

chinagdszsuby · 2010-07-25 14:49 · java基础 · 编程语言 · Markdown

class OtherClass 
{ 
	int i = 0; 

	OtherClass() 
	{ 
		i ++; 
	} 
} 

public class TestOther 
{ 
	public static void main(String[] args) throws Exception 
	{ 
		//方法一
		OtherClass o = new OtherClass(); 
		System.out.println("o.i = " + o.i); 

		//方法二
		Class c = Class.forName("OtherClass"); 
		OtherClass t = (OtherClass)c.newInstance(); 
		System.out.println("t.i = " + t.i); 
	} 
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。