错误--使用Java 8的Eclipse中的超类构造函数
我在eclipse中编写代码,并且是java新手,继承了方法,并使用错误--使用Java 8的Eclipse中的超类构造函数,java,eclipse,inheritance,java-8,super,Java,Eclipse,Inheritance,Java 8,Super,我在eclipse中编写代码,并且是java新手,继承了方法,并使用super()关键字调用父方法,显示java-8中的错误以及new Child()方法也显示错误。文件名为Example.java public class Example{ class Parent{ void parentMethod(){ System.out.println("Parent Method"); }
super()
关键字调用父方法,显示java-8中的错误以及new Child()代码>方法也显示错误。文件名为Example.java
public class Example{
class Parent{
void parentMethod(){
System.out.println("Parent Method");
}
void parentMethod(int a){
System.out.println("Parent Method: One Argument");
}
void parentMethod(int a, int b){
System.out.println("Parent Method: Two Argument");
}
}
class Child extends Parent{
void childMethod() {
super(10);
System.out.println("Child Method");
}
}
public static void main(String[] args) {
new Child();
System.out.println("Main Class Method: no argument");
}
}
它在eclipse中给出了一个错误:
in line 15: Multiple markers at this line
- Line breakpoint:Public$Child [line: 15] -
childMethod()
in line 20: No enclosing instance of type Public is accessible. Must qualify the allocation with an enclosing
instance of type Public (e.g. x.new A() where x is an instance of Public).
。
;
.
super(10)代码>
只能在构造函数内部(而不是在方法内部)用于调用超类(被扩展的类)的构造函数,例如,如果您有Parent(int a){…}
,则可以执行Child(){super(10);}
;或者,如果要调用超类的现有方法(而不是带有单个数字参数的不存在的超级构造函数),请使用super.parentMethod(10)代码>取而代之
新建子项()
在非静态
嵌套类的静态
方法中不起作用;非静态内部类属于外部类的实例:newexample().newchild()代码>将在这里工作;或者,由于在示例中嵌套的类不使用example
的实例方法和字段,因此可以使类Parent
和Child
静态以修复错误:静态类Parent{
和静态类Child扩展Parent{
这不是如何声明公共类公共类父类和公共类子类扩展父类仍然会出错为什么您首先要使用内部类?目的是什么?您想用super(10);
做什么?