Java 类文件中的命名冲突
这里有两个文件,在StaticSuper类(文件2)中,StaticSuper方法上似乎有一个命名clonflict。为什么会这样Java 类文件中的命名冲突,java,Java,这里有两个文件,在StaticSuper类(文件2)中,StaticSuper方法上似乎有一个命名clonflict。为什么会这样 public class StaticTests extends StaticSuper { static int rand; static { rand = (int) (Math.random() * 6); System.out.println("static block
public class StaticTests extends StaticSuper {
static int rand;
static {
rand = (int) (Math.random() * 6);
System.out.println("static block " + rand);
}
StaticTests() {
System.out.println("constructor");
}
public static void main(String [] args) {
System.out.println("in main");
StaticTests st = new StaticTests();
}
}
class StaticSuper {
static {
System.out.println("Super static block");
}
//naming conflict here
StaticSuper{
System.out.println("super constructor");
}
}
这是因为您不正确地构造了类。类构造函数应如下所示
class StaticSuper {
public StaticSuper (){
System.out.println("super constructor");
}
}
public是可选的,但括号应始终存在。学习一点这是一个构造函数吗?@SotiriosDelimanolis根据
System.out.println(“超级构造函数”)判断我想我们可以安全地假设:是的。这看起来像是一个简单的印刷问题(构造函数声明中缺少()
),您忘记了构造函数声明中的括号。