2014-01-26 1 views
0

의 사용에 가장 좋은 방법은 내가 클래스Java 반환 유형이 Object입니다. 초기화에 대해 불평합니다. 시도 및 캐치

public Flight getFlight(int flightNumber){ 
    Flight flightFound; 
    try{ 
     for (int i=0; i<fl.length; i++){    
      if(fl[i].getFlightNumber()==flightNumber){ 
       flightFound= fl[i]; 
      } 
     } 
    }catch(Exception e){ 
     System.out.println("The object does not exist"); 
    } finally{ 
     return flightFound; 
    } 
} 

학습 운동 내에서 방법의 목적에서이 공용 방법이, 방법에 전달 항공편 번호와 해당 개체를 반환하는 것입니다. catch 블록과 같은 좋은 오류 검사는 무엇입니까? 또한 현재 메서드는 returnFoundFound가 초기화되지 않을 수도 있다고 불평합니다.

그래서 내 질문에 개체를 반환하는 몇 가지 좋은 사례가 될 것이라고 및 개체가 존재하지 않는 경우 오류 검사?

public class Manager { 
    Flight[] fl; 
    Ticket[] ticket; 

    public void createFlights(){ 
     this.fl = new Flight[5]; 
     for (int i=0; i<5; i++){ 
      fl[i]= new Flight(); 
     } 

     this.fl[0].Flight(1030, "Toronto", "Kolkata", "03/02/14 7:50 pm", 250, 1450.00); 
     this.fl[1].Flight(1040, "Toronto", "Lahore", "03/02/14 7:10 pm", 250, 1450.00); 
     this.fl[2].Flight(1050, "Toronto", "Kolkata", "03/02/14 1:50 pm", 250, 1450.00); 
     this.fl[3].Flight(1060, "Toronto", "Lahore", "03/02/14 1:10 pm", 250, 1450.00); 
     this.fl[4].Flight(1070, "New York", "Kolkata", "03/02/14 4:50 pm", 250, 1450.00); 
    } 

    public void displayAvailableFlights(String origin, String destination){ 
     for(int i=0; i<5; i++){ 
      if ((this.fl[i].getOrigin().equals(origin)) &&(this.fl[i].getDestination().equals(destination))){ 
       if (this.fl[i].getNumberOfSeatsLeft()>0){ 
        System.out.println(this.fl[i].toString()); 
       } 
      } 
     } 
    } 

    public Flight getFlight(int flightNumber){ 
     Flight flightFound = new Flight(); 
     try{ 
      for (int i=0; i<fl.length; i++){ 

       if(fl[i].getFlightNumber()==flightNumber){ 
        flightFound= fl[i]; 
       } 
      } 
     } 
     catch(Exception e){ 
       System.out.println("The object does not exist"); 
     } 
     finally{ 
      return flightFound; 
     } 
    } 

    public static void main(String[] args){ 
     Manager man = new Manager(); 
     Flight flightFound = new Flight(); 
     man.createFlights(); 
     man.displayAvailableFlights("Toronto", "Kolkata"); 
     flightFound=man.getFlight(030); 
     System.out.println(flightFound.toString()); 
    } 
} 
+0

'fl [i]'가 null 일 가능성이 있습니까? – Christian

+1

먼저 try 블록에 어떤 메소드가 예외를 던질 수 있습니까? 배열'fl'에있는'Flight' 중 하나가 null입니까? 그렇다면 코드를 던지기보다는 널 값을 검사해야합니다 (그래서 try/catch를 전혀 필요로하지 않을 것입니다). 그런 다음 항공편 번호를 찾을 수없는 경우 수행 할 작업을 결정해야합니다. null이 반환되거나 예외가 throw되는 것은 옵션입니다. 마지막으로 'finally'블록이 실제로 필요하지 않습니다. –

+0

한 가지 더. 원시 배열 작업은 괜찮지 만 [Java 모음] (http://docs.oracle.com/javase/tutorial/collections/)이 훨씬 강력하고 객체를 구성하는 여러 가지 방법을 제공한다는 것을 알게 될 것입니다. –

답변

0

flightFound가 시작되지 않을 수있는 두 개의 신화가 있기 때문에 컴파일러가 초기화를 요구합니다.

fl.length = 0

또는

FL [I] .getFlightNumber()! = flightNumber 모든 배열 인덱스에

0

행 flightFound가 getFlight (INT의 flightNumber) 방법 및 국부적 인 try 블록에서 초기화되고 try 블록에서 예외가 발생하면 컨트롤이 catch 블록으로 전송되고 flightFound가 초기화되지 않을 수 있습니다. 또는 fl [i] .getFlightNumber() == flightNumber가 false 인 경우 flightFound가 초기화되지 않을 수 있습니다.