gpt4 book ai didi

Java Eclipse - 未定义的构造函数和方法

转载 作者:行者123 更新时间:2023-12-01 17:49:27 24 4
gpt4 key购买 nike

错误位于底部的主类中,我在代码中详细说明了它们及其错误消息。我的 Session 构造函数中还有一个 super() 关键字,用于调用 Customer 类中的方法。我只想运行我的程序,但每次我解决一个问题时,另一个(或两个)问题就会取代它。请帮忙!

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 setPlansLeft(int pl)
{
plansLeft = pl;
}


public String getName()
{
return Name;
}
public int getIDNumber()
{
return ID;
}
public int getPlansLeft()
{
return plansLeft;
}
}


public class Session extends Customer
{
public int sessionIDNumber = 0000;
public int spotsLeft = 0;

public Session()
{
}

public Session (String Name, int ID, int plansLeft, int sID, int sl)
{
super(Name, ID, plansLeft); // <----- perhaps an issue here?
sessionIDNumber = sID;
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)
{
//public Customer(){} // <----- tried using this but only adds more errors

Customer customer = new Customer("Sarah", 12345678, 12, 0117, 0); // < ----- error is here: "The constructor Main.Customer(String, int, int, int, int) is undefined"

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()); // < ------ couple more errors here: "The method getsessionIDNumber/getspotsLeft() is undefined for the type Main.Customer"
}
}

最佳答案

将 Customer 类定义为静态。这是示例:-

  public class Main 
{
public static 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 setPlansLeft(int pl)
{
plansLeft = pl;
}


public String getName()
{
return Name;
}
public int getIDNumber()
{
return ID;
}
public int getPlansLeft()
{
return plansLeft;
}
}


public class Session extends Customer
{
public int sessionIDNumber = 0000;
public int spotsLeft = 0;

public Session()
{
}

public Session (String Name, int ID, int plansLeft, int sID, int sl)
{
super(Name, ID, plansLeft); // <----- perhaps an issue here?
sessionIDNumber = sID;
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)
{
//public Customer(){} // <----- tried using this but only adds more errors

Customer customer = new Customer("Sarah", 12345678, 12); // < ----- error is here: "The constructor Main.Customer(String, int, int, int, int) is undefined"

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()); // < ------ couple more errors here: "The method getsessionIDNumber/getspotsLeft() is undefined for the type Main.Customer"
}
}

关于Java Eclipse - 未定义的构造函数和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com