Java returnBooking()方法应该做什么?

Java returnBooking()方法应该做什么?,java,class,return,Java,Class,Return,在我从另一个类调用这个后,我尝试返回到我的上一个类,但它出现了这个错误,下面是我的代码 public class Train extends Booking { public Train returnBooking(){ int Tid,dest; Date now = new Date(); Scanner sc= new Scanner(System.in); Scanner sc2=new Scanner(System.in); try{

在我从另一个类调用这个后,我尝试返回到我的上一个类,但它出现了这个错误,下面是我的代码

public class Train extends Booking {
  public Train returnBooking(){
     int Tid,dest;
     Date now = new Date();
     Scanner sc= new Scanner(System.in);
     Scanner sc2=new Scanner(System.in);
 try{     
     System.out.println("Enter train ID");
     System.out.println("(**Example '1001'**)");
     Tid=sc.nextInt();
     System.out.println("1. Malacca Zoo to Night Safari");
     System.out.println("2. Malacca Zoo to River Safari");
     System.out.println("3. River Safari to Malacca Zoo");
     System.out.println("Enter a destination for the train");
     dest = sc.nextInt();
     System.out.println("Enter date: ");
     String date = sc2.nextLine();

     SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");  
     Date testDate = null;  


     try{

        testDate = df.parse(date);    
    }catch(ParseException e){ System.out.println("invalid format");} 
    if (!df.format(testDate).equals(date)){  
        System.out.println("invalid date!!");  
    } else {  
        System.out.println("valid date");  
    } 
}catch(InputMismatchException e){System.out.print("Input must be numbers only");}
return Booking.class;**//imcompatible type**
 }
}
我的退货方法错了吗?或者只是一些我想念的简单错误

这是我的订舱代码

public class Booking{
public static void main(String[]agrs){
   Scanner sc = new Scanner(System.in);

   int sel,book_ticket, reschedule, cancel, add, remove, display, total, loop;

   System.out.println("\t\t\t\t\t****************************************************\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*              Welcome to Malacca Zoo              *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*  This is our new online train reservstion system *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*  follow the instruction at below to perform your *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t* reservation, hope you will enjoy our new services*\t\t\t\t\t");
   System.out.println("\t\t\t\t\t****************************************************\t\t\t\t\t");
   System.out.println("\n\n1.Select and book a ticket ");
   System.out.println("2.Reschedule a train");
   System.out.println("3.Cancel a reservation");
   System.out.println("4.Add new train service");
   System.out.println("5.Remove a train from service");
   System.out.println("6.Display total number of passengers");
   System.out.println("7.Total profit from a service");
   System.out.println("0.Exit");
   System.out.println("\nEnter the number to select the function that you want to perform");

//try {
   sel=sc.nextInt();
   for(loop=0;loop<2;loop++)   {
      if (sel==0)
      {
          System.exit(0);  
      }
      else if(sel == 4)
      {
         new Train();
      }
      else
      {
           System.out.println("You enter an invalid number. Please try again");
           sel=sc.nextInt();
           loop=0;
       }
    }
 // } catch (InputMismatchException e){
 // } finally {
 // }
 // System.out.print("Input must be a number between 1 and 0: ");
}
}

您定义了在返回类时返回火车类型的方法

尝试将代码更改为以下内容:

public Train returnBooking(){
    //code
    Train trainObject = new Train();
    // code to fill the Train object.
    return new trainObject();
}
或者干脆什么也不返回,因为您没有对任何Train对象进行任何处理

或者我建议将您的方法声明为void,因为您似乎没有对Train对象执行任何操作。你甚至没有在火车或订票课上申报任何物品


在提问之前,请确保您具有正确的java知识

如何返回java.lang.Class类型而不是Train???类型的java.lang.Object


编译器肯定会为你的代码哭泣。

是的,它错了。编译器告诉你这是错误的。该方法应返回一个对象ot类型序列。您将返回java.lang.Class类型的对象。在这种情况下,我应该如何返回到以前的类?MorganSim的问题可能是returnBooking方法应该做什么?也许您是对的,我应该更改我的question@MorganSIm:如果您不知道自己的程序应该做什么,我们怎么知道呢?从一个应该返回火车的方法返回预订不会有多大帮助;是函数,但当我运行代码时,它不会显示任何内容,只是跳过此部分。从void方法返回预订也不会编译。它变为无法返回结果类型为的值形式方法void@JBNizet如果您阅读了我上面关于使方法无效的评论,您应该知道我忘记删除return语句了。
return null;
public void returnBooking(){
    //code
}