我遇到语法错误 “令牌“ {”的语法错误,{预期在此令牌之后” 和 在客户类中使用super()关键字后,出现“语法错误,插入“}”以完成阻止”。我在其他论坛帖子中四处浏览,但是在我的Customer类中我已经把super()作为第一件事。我该如何解决?
import java.util.*;
public class Main
{
public class Customer
{
public String Name = "";
public int ID = 0000;
public int plansLeft = 0;
public Customer()
{
}
public Customer (String n, int id, int pl)
{
Name = n;
ID = id;
plansLeft = pl;
}
public void setName(String n)
{
Name = n;
}
public void setidnumber(int id)
{
ID = id;
}
public void setdepartmen(int pl)
{
plansLeft = pl;
}
public String getName()
{
return Name;
}
public int getIDNumber()
{
return ID;
}
public int getPlansLeft()
{
return plansLeft;
}
}
public class Session extends Customer
{
super(Name, ID, plansLeft); // <------------------------- issue is here
public int sessionIDNumber = 0000;
public int spotsLeft = 0;
public Session()
{
}
public Session (int cID, int sl)
{
sessionIDNumber = cID;
spotsLeft = sl;
}
public int getsessionIDNumber() {
return sessionIDNumber;
}
public int getspotsLeft() {
return spotsLeft;
}
public void setsessionIDNumber(int sessionIDNumber) {
this.sessionIDNumber = sessionIDNumber;
}
public void setspotsLeft(int sl) {
this.spotsLeft = sl;
}
}
public static void main(String[] args)
{
Session session = new Session(0117, 1);
Customer customer = new Customer("Sarah", 12345678, 12);
System.out.println("Customer Name: " + customer.getName() + "\nCustomer ID: " + customer.getIDNumber() + "\nLessons Remaining: " + customer.getPlansLeft());
System.out.println("\nSession ID: " + session.getsessionIDNumber() + "\nOpen Spots Available: " + session.getspotsLeft());
}
}
答案 0 :(得分:2)
您必须将一些值传递给超类构造函数。并且应该在构造函数之后的第一行调用super
它应该看起来像这样
public Session (int cID, int sl, String Name, int id, int plansLeft)
{
super(Name, id, plansLeft);
sessionIDNumber = cID;
spotsLeft = sl;
}
主要
public static void main(String[] args)
{
Customer customer = new Customer(0117, 1, "Sarah", 12345678, 12);
System.out.println("Customer Name: " + customer.getName() + "\nCustomer ID: " + customer.getIDNumber() + "\nLessons Remaining: " + customer.getPlansLeft());
System.out.println("\nSession ID: " + customer.getsessionIDNumber() + "\nOpen Spots Available: " + customer.getspotsLeft());
}
}
答案 1 :(得分:0)
您要
public Session() {
super(Name, ID, plansLeft);
}
super()作为对父构造函数的调用必须是子构造函数的第一行。