我收到错误 'java.lang.String' 并且不知道为什么
Im getting Error 'java.lang.String' and not knowing why
我是 Java 的新手。所以问题听起来很简单,但我被卡住了,无法弄清楚为什么我会收到错误 java.lang.String,尽管我所做的只是正常地写出 setter 和 getter。下面是我的代码:
package PerSion;
public class Student extends Persion{
String RollNum, Class;
double Score;
public String getRollNum() {
return this.RollNum;
}
public void setRollNum(String RollNum) {
this.RollNum = RollNum;
}
public String getClass() { //this is where the error show up
return this.Class;
}
public void setClass(String Class) {
this.Class = Class;
}
public double getScore() {
return this.Score;
}
public void setScore(double Score) {
this.Score = Score;
}
public Student() {
}
public Student(String RollNum, String Class, double Score) {
this.RollNum = RollNum;
this.Class = Class;
this.Score = Score;
}
public Student(String RollNum, String Class, double Score, String ID, String Name, String Address, int Age) {
super(ID, Name, Address, Age);
this.RollNum = RollNum;
this.Class = Class;
this.Score = Score;
}
@Override
public void Display() {
System.out.println(RollNum+"\t"+Name+"\t"+Class+"\t"+Score);
}
}
你的问题是每个 class 已经有一个 final
方法叫做 getClass
。您必须将您的方法命名为其他名称,因为在 class 中不能有两个具有完全相同名称和相同参数的方法,并且您不能覆盖 final
方法。
package PerSion;
public class Student extends Persion{
String RollNum, Class;
double Score;
public String getRollNum() {
return this.RollNum;
}
public void setRollNum(String RollNum) {
this.RollNum = RollNum;
}
public String getClass() { //this is where the error show up
return this.Class;
}
public void setClass(String Class) {
this.Class = Class;
}
public double getScore() {
return this.Score;
}
public void setScore(double Score) {
this.Score = Score;
}
public Student() {
}
public Student(String RollNum, String Class, double Score) {
this.RollNum = RollNum;
this.Class = Class;
this.Score = Score;
}
public Student(String RollNum, String Class, double Score, String ID, String Name, String Address, int Age) {
super(ID, Name, Address, Age);
this.RollNum = RollNum;
this.Class = Class;
this.Score = Score;
}
@Override
public void Display() {
System.out.println(RollNum+"\t"+Name+"\t"+Class+"\t"+Score);
}
}
你的问题是每个 class 已经有一个 final
方法叫做 getClass
。您必须将您的方法命名为其他名称,因为在 class 中不能有两个具有完全相同名称和相同参数的方法,并且您不能覆盖 final
方法。