不是说newInstance调用缺省的构造方法吗????
为什么我的程序还是能运行过去呢?
class Test
{
public static void main(String[] args)
{
if(args.length!=1)
{
return;
}
try
{
Class c=Class.forName(args[0]);
Point pt=(Point)c.newInstanc();
pt.output();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Point
{
static
{
System.out.println("Loading Point");
}
int x,y;
void output()
{
System.out.println("x="+x+","+"y="+y);
}
Point(int x,int y )
{
this.x=x;
this.y=y;
}
}
在我的电脑上javac和java都能运行过去
应该运行java的时候抛出异常啊????
我哪里写错了呢???
谢谢大家了!!!我好象没有多少分.
为什么我的程序还是能运行过去呢?
class Test
{
public static void main(String[] args)
{
if(args.length!=1)
{
return;
}
try
{
Class c=Class.forName(args[0]);
Point pt=(Point)c.newInstanc();
pt.output();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Point
{
static
{
System.out.println("Loading Point");
}
int x,y;
void output()
{
System.out.println("x="+x+","+"y="+y);
}
Point(int x,int y )
{
this.x=x;
this.y=y;
}
}
在我的电脑上javac和java都能运行过去
应该运行java的时候抛出异常啊????
我哪里写错了呢???
谢谢大家了!!!我好象没有多少分.
2, Did you type this line correctly?
Point pt=(Point)c.newInstanc();
3, Does your class Point have a default (zero argument) constructor?
Loading Point
java.lang.InstantiationException: Point
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at Test.main(Test.java:12)