2017-12-04 1 views
-1

구현할 목록 클래스를 결정하는 데 사용되는 문자열 변수 type을 사용하는 메서드를 작성했습니다. 이 메서드는 switch 문을 사용하여 변수 이름 newList을 사용하여 적절한 유형의 목록을 만듭니다.switch 문 안에 객체를 만드는 데 문제가있는 경우 "객체를 확인할 수 없습니다"(자바)

스위치 문은 다음과 같습니다

switch (type) { 
    case "unsorted": { 
     UnsortedList newList = new UnsortedList(); 
     break; 
    } 
    case "sorted": { 
     SortedList newList = new SortedList(); 
     break; 
    } 
    case "new sorted": { 
     NewSortedList newList = new NewSortedList(); 
     break; 
    } 
    default: { 
     System.out.print("Something went wrong! List type unrecognized. I have initialized newList to be an unsorted list."); 
     UnsortedList newList = new UnsortedList(); 
    } 
} 

이에 따라, 나는 newList 항상 인해 switch 문의 기본 케이스에 인스턴스화 할 것이라고 믿는다. 그러나 "newList를 (를) 확인할 수 없습니다"라는 오류가 발생합니다.

내가 이것을 막기 위해 할 수있는 일이 있습니까?

+2

'newList'의 범위는 개별'{}'블록입니다. 다른 곳에서는 볼 수 없습니다. –

+3

스위치 내부에 목록을 만들었으므로 외부에서 액세스 할 수 없으므로 범위를 벗어났습니다 – azro

+0

어떻게'newList'를 사용하고 있습니까? 이 클래스들은 공통 인터페이스를 공유합니까? – shmosel

답변

0

newList이 너무 작은 범위로 생성되고 있습니다. Java의 범위는 {}입니다. 변수는 생성 범위와 동일한 수준 또는 그 이상의 범위에 대해서만 존재합니다. 예를 들어

:

List newlist; // make sure all of your various List types are compatible. 
switch (type){ 
    case "unsorted":  
    { 
     newList = new UnsortedList(); 
     break; 
    } 
    case "sorted":  
    { 
     newList = new SortedList(); 
     break; 
    } 
    case "new sorted": 
    { 
     newList = new NewSortedList(); 
     break; 
    } 

    default: 
    { 
     System.out.print("Something went wrong! List type unrecognized. I have initialized newList to be an unsorted list."); 
     newList = new UnsortedList(); 
    } 
} 
0

이전의 두 가지 주석 모두 질문에 답했습니다. 즉시 다른 범위를 벗어나는 몇 가지 다른 로컬 newList 필드를 만듭니다.

해결책은 스위치 외부에 List newList를 만드는 것입니다. 다만,리스트로 지정된 메소드에는 제한이 있기 (위해) 때문에, 소트 메소드에 액세스 할 수 없게됩니다.

0

나는 List라는 목록 유형의 인터페이스를 구현 유형으로 newList을 인스턴스화 :

public class ScopeClass(){ //this is the start of the class level scope 
    private String classString = "this can been seen anywhere in the class"; 
    public void scopeMethod(){ //this starts the method level scope 
     private String methodString = "this is only visible in the method"; 
     { //start of internal scope block 
      private String internalString = "only visible in this scope block"; 
     } 
    } 
} 

switch 범위의 당신의 newList 외부에 표시하려면이 시도 switch 문 앞에 List을 입력하십시오. 이로 인해 스위치 외부에서 newList을 볼 수 있었고 여전히 각 목록 유형의 속성을 얻을 수있었습니다.

여기 내 코드의 수정 된 버전입니다 :

List newList; 
    switch (type) 
    { 
     case "unsorted":  
     { 
      newList = new UnsortedList(); 
      break; 
     } 
     case "sorted":  
     { 
      newList = new SortedList(); 
      break; 
     } 
     case "new sorted": 
     { 
      newList = new NewSortedList(); 
      break; 
     } 
     default: 
     { 
      System.out.print("Something went wrong! I have initialized newList to be an unsorted list"); 
      newList = new UnsortedList(); 
     } 
    } 

당신의 도움에 대해 모두에게 감사드립니다. 나는 아직도 모든 의견이 도움이되도록 배우고있다!

+0

신입 회원이므로 질문에 대한 최선의 답변에 투표하고 동의하십시오. 이렇게하면 다른 사람들이 비슷한 질문을하는 경우 쉽게 답을 찾을 수 있습니다. –

관련 문제