2014-04-03 1 views
0

다른 클래스에서 HashMap에 사용할 수 있도록 관련 문제는, 내가 잘못 무슨 일이 일어나고 있는지 확실하지 않다 :자바 - 내가 그러나이 현재의 문제에 대한 답을 많이 연구 한

import java.util.Map; 
    import java.util.HashMap; 

    public class Items 
    { 
     public static void main (String args[]) 
     { 
     HashMap<String, Double> Hallway = new HashMap<String, Double>(); 
     HashMap<String, Double> Toilet = new HashMap<String, Double>(); 
     HashMap<String, Double> ChemistryLab = new HashMap<String, Double>(); 
     HashMap<String, Double> Outdoors = new HashMap<String, Double>(); 
     HashMap<String, Double> Library = new HashMap<String, Double>(); 
     HashMap<String, Double> Engineering = new HashMap<String, Double>(); 
     HashMap<String, Double> Cafeteria = new HashMap<String, Double>(); 
     HashMap<String, Double> ComputerLab = new HashMap<String, Double>(); 
     HashMap<String, Double> LectureTheater = new HashMap<String, Double>(); 
     HashMap<String, Double> MedicalCentre = new HashMap<String, Double>(); 
     } 

    public HashMap<String, Double> getHallwayItems() 
    { 
     return Hallway;   
    } 

    public HashMap<String, Double> getToiletItems() 
    { 
     return Toilet; 
    } 

    public HashMap<String, Double> getChemistryLabItems() 
    { 
    return ChemistryLab; 
    } 

    public HashMap<String, Double> getOutdoorItems() 
    { 
    return Outdoors; 
    } 

    public HashMap<String, Double> getLibraryItems() 
    { 
    return Library; 
    } 

    public HashMap<String, Double> getEngineeringItems() 
    { 
    return Engineering; 
    } 

    public HashMap<String, Double> getCafeteriaItems() 
    { 
    return Cafeteria; 
    } 

    public HashMap<String, Double> getComputerLabItems() 
    { 
    return ComputerLab; 
    } 

    public HashMap<String, Double> getLectureTheaterItems() 
    { 
    return LectureTheater; 
    } 

    public HashMap<String, Double> getMedicalCentreItems() 
    { 
    return MedicalCentre; 
    } 

    } 

그것은 말한다 그러나 컴파일을 시도 할 때 변수 Hallway를 찾을 수 없지만 어떻게 해결 될지 알 수 없습니다. 도움을 주셔서 감사합니다.

+2

당신은 [범위] (http://en.wikipedia.org/wiki/Scope_ (computer_science)) – azurefrog

+0

그리고에 대한 표준에 대해 배울 필요가 ... OOPS 악용 Java 명명 규칙. –

답변

1

Hallway은 main 함수 내부의 로컬 변수입니다. 그리고 main도 정적 함수입니다. main 안에 선언 된 변수는 다른 함수에서 액세스 할 수 없습니다.

나는 개인적으로이 제안 두 가지 대안,

1) 객체의 정적 멤버로 모든 맵을 선언하고 정적 getHallway() 정적 getToilet이 등 2),

수입 자바가 있습니다. util.Map; import java.util.HashMap;

public class Items 
{ 
    private HashMap<String, Double> Hallway = new HashMap<String, Double>(); 
    private HashMap<String, Double> Toilet = new HashMap<String, Double>(); 
    ... 

    public HashMap<String, Double> getHallwayItems() 
    { 
     return Hallway;   
    } 
    ... 
    ... 

    public static void main (String args[]) 
    { 
     Items myItem = new Items(); 
     myItem.getHallwayItems(); // and do whatever you want. 
    } 
} 

이 방법, 우리는 또한

1

Hallway 및 기타를 인스턴스 변수로 정의해야합니다. 변수는 다른 메소드에서 액세스 할 수없는 메소드 범위에 정의되었습니다.

import java.util.Map; 
import java.util.HashMap; 

public class Items 
{ 
    HashMap<String, Double> Hallway = new HashMap<String, Double>(); 
    HashMap<String, Double> Toilet = new HashMap<String, Double>(); 
    HashMap<String, Double> ChemistryLab = new HashMap<String, Double>(); 
    HashMap<String, Double> Outdoors = new HashMap<String, Double>(); 
    HashMap<String, Double> Library = new HashMap<String, Double>(); 
    HashMap<String, Double> Engineering = new HashMap<String, Double>(); 
    HashMap<String, Double> Cafeteria = new HashMap<String, Double>(); 
    HashMap<String, Double> ComputerLab = new HashMap<String, Double>(); 
    HashMap<String, Double> LectureTheater = new HashMap<String, Double>(); 
    HashMap<String, Double> MedicalCentre = new HashMap<String, Double>(); 
    public static void main (String args[]) 
    { 

    } 

public HashMap<String, Double> getHallwayItems() 
{ 
    return Hallway;   
} 

public HashMap<String, Double> getToiletItems() 
{ 
    return Toilet; 
} 

public HashMap<String, Double> getChemistryLabItems() 
{ 
return ChemistryLab; 
} 

public HashMap<String, Double> getOutdoorItems() 
{ 
return Outdoors; 
} 

public HashMap<String, Double> getLibraryItems() 
{ 
return Library; 
} 

public HashMap<String, Double> getEngineeringItems() 
{ 
return Engineering; 
} 

public HashMap<String, Double> getCafeteriaItems() 
{ 
return Cafeteria; 
} 

public HashMap<String, Double> getComputerLabItems() 
{ 
return ComputerLab; 
} 

public HashMap<String, Double> getLectureTheaterItems() 
{ 
return LectureTheater; 
} 

public HashMap<String, Double> getMedicalCentreItems() 
{ 
return MedicalCentre; 
} 

} 
관련 문제