Java-找不到Contstructor的符号
运行时:javac GrammerStack.java 我得到以下信息:Java-找不到Contstructor的符号,java,Java,运行时:javac GrammerStack.java 我得到以下信息: import java.lang.*; public class GrammerStack extends GrammerStructure implements StringStack { private String structName; private int cap; public GrammerStack(String structureName, int limit){ this.struct
import java.lang.*;
public class GrammerStack extends GrammerStructure implements StringStack {
private String structName;
private int cap;
public GrammerStack(String structureName, int limit){
this.structName = structureName;
this.cap = limit;
System.out.println(structName+"["+cap+"]");
}
public void GrammerStructure(String structureName){
this.structName = structureName;
}
//Empty overrides.
public String[] asArray(){};
public String push(String item) throws FillException{};
public String pop() throws EmptyException{};
public boolean contains(String query){};
public void empty(){};
public double fillPercent(){};
public String getName(){ return structName; }
public void main(String args[]){
GrammerStack("Stack1",3);
}
}
我不知道这里出了什么问题,我的方法也没有与文件名不匹配。“Grammer”在这种情况下是正确的
有帮助吗?发生此错误的原因是,在超类构造函数中,您尚未定义无参数或默认构造函数
GrammerStack.java:15: cannot find symbol
symbol : constructor GrammerStructure()
location: class GrammerStructure
public GrammerStack(String structureName, int limit){
^
GrammerStack.java:41: cannot find symbol
symbol : method GrammerStack(java.lang.String,int)
location: class GrammerStack
GrammerStack("Stack1",3);
^
2 errors
当您在超类中定义带有参数的构造函数时,您也必须定义无参数构造函数,原因如下:编译器不会为您添加默认构造函数
子类将抛出您刚才提到的错误,因为在子类构造函数中,javac将添加一个默认的
super()
调用。对于第一个问题,添加super(structureName)代码>作为构造函数中的第一行。我假设您在超类中定义了公共语法结构(字符串名)
构造函数。如果不是这样,请共享您的构造函数
GrammerStack.java:15: cannot find symbol
symbol : constructor GrammerStructure()
location: class GrammerStructure
public GrammerStack(String structureName, int limit){
^
GrammerStack.java:41: cannot find symbol
symbol : method GrammerStack(java.lang.String,int)
location: class GrammerStack
GrammerStack("Stack1",3);
^
2 errors
对于第二个问题,您缺少关键字new
。更改如下:
public GrammerStack(String structureName, int limit){
super(structureName);
this.structName = structureName;
this.cap = limit;
System.out.println(structName+"["+cap+"]");
}
与
或
您不能定义无参数的默认构造函数。当您在超类中定义带参数的构造函数时,必须定义无参数的默认构造函数。当您在超类和子类中定义构造函数时,需要遵循一些规则:-
- 如果在这两个类中都没有定义任何构造函数,那么编译器将在这两个类中都添加一个默认构造函数。。那么,你没事了
- 如果您在超类中定义了一个0-arg构造函数,您仍然可以。因为,从子类中的任何构造函数,如果您没有调用任何超类构造函数,编译器将自动添加一个
super()
调用,作为每个子类构造函数的第一个语句。(注意:-编译器只添加super
调用0-arg构造函数)
- 如果您在超类中没有定义0-arg构造函数,但是有一个参数化构造函数,那么如果您没有从每个子类构造函数显式调用该参数化构造函数,您可能会遇到问题。。这是因为,编译器只添加对0-arg构造函数的调用,而您在那里并没有调用
因此,代码的问题是第三个问题。
解决问题:-
- 在超类中添加一个0-arg构造函数
import java.lang.*; public class GrammerStack extends GrammerStructure implements StringStack { private String structName; private int cap; public GrammerStructure() { } public GrammerStack(String structureName, int limit){ this.structName = structureName; this.cap = limit; System.out.println(structName+"["+cap+"]"); } public void GrammerStructure(String structureName){ this.structName = structureName; } //Empty overrides. public String[] asArray(){}; public String push(String item) throws FillException{}; public String pop() throws EmptyException{}; public boolean contains(String query){}; public void empty(){}; public double fillPercent(){}; public String getName(){ return structName; } public void main(String args[]){ GrammerStack("Stack1",3); } }
- 或者,在每个子类构造函数中,向参数化超类构造函数添加一个
调用super
public GrammerStructure() { }
new
关键字来实例化类。。因此,在main方法中,应该使用以下内容更改调用:-
public GrammerStack(String structureName, int limit) {
// Add a `super` with some parameters to match your parameterized
// super class constructor here..
// It should be the first statement..
}
我试着加上:public GrammerStack(){};但这并没有消除错误。我相信您已经在超类
GrammerStructure
中定义了一些构造函数。我假设它采用structureName
作为参数。如果是,则添加super(structureName)
作为类构造函数方法的第一行public GrammerStack(String structureName,int limit){
。我更新了答案。如果不是这样,请在GrammersStructure
类中共享您的构造函数。当我将代码更改为以下内容以包括super()它仍然给我一个错误,但只在一个空的构造函数上。如果没有其他两个构造函数中的super,它仍然会抱怨它们,但是对于super,这两个都没有提到。public GrammerStack(){}public GrammerStack(String structureName){super(structureName);}public GrammerStack(String structureName,int limit){super(structureName);}请提供GrammerStructure
的代码。请共享您的GrammerStructure
类构造函数。
public GrammerStructure() {
}
public GrammerStack(String structureName, int limit) {
// Add a `super` with some parameters to match your parameterized
// super class constructor here..
// It should be the first statement..
}
public void main(String args[]){
new GrammerStack("Stack1",3);
}