2017-12-02 17 views
2

클래스 자바 확장 "액세서리은"다음이다개체 ArrayList에 내가 이름을 문자열과 논리 값을 포함하는 객체의 그룹으로 클래스를 생성 한

후 생성 된 ArrayList 클래스는 "이름이 목록에 추가 악세사리리스트 "에서 더 많은 데이터가 입력됩니다.

그런 다음 for 루프를 사용하여 ArrayList의 데이터를받을 Accessories 객체를 만들었습니다. 이것은 여전히 ​​null로 응답합니다.

저는 주위를 둘러 보았고 가장 일반적인 문제는 변수가 초기화되지 않았다는 것입니다. 그래서 나는 시도하고 아직도 수신,

그래서 여기에 같은 결과를 얻고있는 액세서리를 클래스를

다음
public static class Accessories { 

    Accessories(String Accessoriesname, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    } 
    String name =null ; boolean cold; boolean warm; boolean hot; boolean rain; boolean snow; boolean ice; boolean formal; boolean informal; 
} 

AccessoriesList 클래스

public ArrayList createAccessories() { 
    ArrayList<Accessories> Accessoriesist = new ArrayList<Accessories>(); 
    Accessoriesist.add(new Accessories("Bag", true, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Gloves", true, false, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Hat", true, false, false, true, true, true, false, true)); 
    Accessoriesist.add(new Accessories("Poncho", false, true, true, false, false, false, false, true)); 
    Accessoriesist.add(new Accessories("Scarf", true, true, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Sunglasses", false, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Tie", true, true, true, true, true, true, true, true)); 

    Accessories getAccessories =null; 
    String getname = null; 
    for (int i = 0; i < Accessoriesist.size(); i++) { 
     getAccessories = Accessoriesist.get(i); 
     getname = getAccessories.name; 
     System.out.println("this is the name : " + getname); 
     System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 
    } 
    return Accessoriesist; 
} 

입니다 대신의 정보를 수신 해시 코드

ArrayList의 액세서리 객체 (orginal)를 다른 액세서리 객체 (new)로 던지려고합니다.

먼저 생성자 복사하지 않습니다 당신이 클래스로에 전달하는 성질 : : 액세서리 (문자열 Accessoriesname, 부울 나는 당신이 두 가지 문제가

답변

0

(신규) 보조 프로그램 개체에서 데이터를 가져 위해 노력하고 있어요 boolean ice, boolean formal, boolean informal) { }

가변 매개 변수가있는 메소드 호출로 생성자를 생각하십시오.이 경우에는 중괄호 {}. Java는 클래스 인스턴스의 특성을 나타 내기 위해 키워드를 제공합니다. 이 코드 줄은 객체와 문자열을 연결하기 때문에, 그것은을 호출,

Accessories(String Accessories name, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    this.name = name 
    this.cold = cold 
    this.warm = warm 
    this.hot = hot 
    this.rain = rain 
    this.snow = snow 
    this.ice = ice 
    this.formal = formal 
    this.informal = informal 
} 

둘째 : 그래서 당신은 명시 적으로 클래스 인스턴스의 특성에 생성자에 전달 된 매개 변수를 복사해야 당신의 액세서리에() 메소드로 .toString 오브젝트 :

System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 

.toString() 메소드의 디폴트 구현은 객체의 슈퍼 클래스로부터 상속됩니다. 당신이 그것을 무시하고 싶은 경우에, 다만 Object.toString()

public String toString() { 
    StringBuilder sb = new StringBuilder(this.name); 
    if (ice) { 
     sb.append(" (ice)") 
    } 
    // other properties 
    return sb.toString() 
} 

몇 최종 노트와 동일한 방법으로 서명하여 클래스에 메소드를 추가 :

  • 낙타 표기법을 사용하여 자바에서 일반적입니다.클래스의 경우 첫 문자 (MyClass)에 대문자 ( )를 사용하고 클래스 변수 및 매개 변수에 대해서는 첫 번째 문자 (myClass)에 소문자를 사용합니다. 따라서 귀하의 ArrayList<Accessories> Accessoriesist 은 규칙 다음에 ArrayList<Accessories> accessoriesList이됩니다.
  • IT는 목록을 포함 Properties 같은 것을 라는 enum를 (등 감기, 따뜻한, 얼음, 눈,) 당신의 다른 의 모든 속성을 만들 수와 액세서리 클래스를 가지고하는 것이 좋습니다 수 있습니다.

Java에 오신 것을 환영합니다!