private String name;
List<a> list = new ArrayList<a>();
{
a a1 = new A();
list.add(a1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestConstructorBlock t = new TestConstructorBlock();
}
}
interface a {
void shwo();
}
class A implements a{
@Override
public void shwo() {
// TODO Auto-generated method stub
System.out.println("this is from A.");
}
}
程序编译的时候没报错,运行的时候,报异常,
Exception in thread "main" java.lang.NoClassDefFoundError: testSomething/A (wrong name: testSomething/a)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at testSomething.TestConstructorBlock.<init>(TestConstructorBlock.java:13)
at testSomething.TestConstructorBlock.main(TestConstructorBlock.java:19)
这个构造代码块中不能用接口向上转型吗?有点晕。作者: 全海波 时间: 2012-7-19 06:11 本帖最后由 全海波 于 2012-7-19 12:44 编辑
package testSomething;
import java.util.ArrayList;
import java.util.List;
class TestConstructorBlock
{
/**
* @param args
*/
// private String name;
public static void main(String[] args)
{
// TODO Auto-generated method stub
//创建本类对象
TestConstructorBlock t = new TestConstructorBlock();