简体   繁体   English

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

[英]Java Eclipse - Undefined constructors and methods

The errors are within the main class at the bottom, I have them detailed within the code along with their error messages. 错误在底部的主类中,我在代码中详细列出了它们以及它们的错误消息。 I also have a super() keyword in my Session constructor to call methods from the Customer class. 我的Session构造函数中也有一个super()关键字,可以从Customer类中调用方法。 I just want to run my program but every time I fix one issue another one (or two) takes its place. 我只想运行我的程序,但是每次我修复一个问题时,另一个(或两个)问题就会取代它。 Please help! 请帮忙!

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"
}
}

Define the Customer class as static. 将客户类定义为静态。 Here is sample:- 这是示例:

  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"
}
}

One thing remember when you use inheritance always create object of child(sub) class. 记住一件事,当您使用继承时,总是创建child(sub)类的对象。 in your case, Session is sub class in which you have five parameters but you created Customer class which not have five parameters. 在您的情况下,Session是其中有五个参数但创建了没有五个参数的Customer类的子类。

Customer customer = new Customer("Sarah", 12345678, 12, 0117, 0);

This line issue an error because Customer class do not have five parameters. 此行发出错误,因为Customer类没有五个参数。

Customer and Session class both present in Main class so their object access through Main class object. Customer和Session类都存在于Main类中,因此它们的对象可通过Main类对象进行访问。

use below code instead of above- 使用下面的代码而不是上面的代码-

Session session= new Main().new  Session("Sarah", 12345678, 12, 0117, 0);

when child class object created it call parent class constructor also. 创建子类对象时,它也会调用父类构造函数。 and for your comment call everything using session object- 对于您的评论,请使用会话对象调用所有内容,

System.out.println("Customer Name: " + session.getName() + "\nCustomer ID: " + session.getIDNumber() + "\nLessons Remaining: " + session.getPlansLeft());
    System.out.println("\nSession ID: " + session.getsessionIDNumber() + "\nOpen Spots Available: " + session.getspotsLeft());

尝试这个:
Customer customer = new Main().new Customer("Sarah", 12345678, 12)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM