2016-10-06 2 views
-1

그래서이 흥미로운 문제가 있습니다 ... ArrayList에 의해 확장 된 List에서 검색되는 객체의 메서드에 액세스하려고합니다.ArrayList 확장의 캐스트 객체에 메서드가 표시되지 않습니다.

은 다음과 같습니다

import java.util.ArrayList; 
import java.util.Collection; 

public class PropertyList<Property> extends ArrayList { 

private static final long serialVersionUID = -7854888805136619636L; 

    PropertyList(){ 
     super(); 
    } 

    PropertyList(Collection<Property> c){ 
     super(c); 
    } 

    boolean containsProperty(PropertyList<Property> pl){ 
     Property asdf = (Property) this.get(4); 
     System.out.println(asdf.<can't access "Property" methods>); //mark 
     return false; 
    } 

} 

어떤 생각이 왜 표시된 라인의 메소드를 액세스 할 수 없습니다? 명확하게하기 - Property 객체의 메서드는 public입니다.

편집 : 대지 설명 :

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlValue; 


@XmlAccessorType(XmlAccessType.FIELD) 
class Property { 

@XmlValue 
private String property = null; 

@XmlAttribute 
String state; 
@XmlAttribute 
String name; 
@XmlAttribute 
String type; 
@XmlAttribute 
String value; 

Property(){} 

Property(String state, String name, String type, String value){ 
    this.state = state; 
    this.name = name; 
    this.type = type; 
    this.value = value; 
} 

public String getName(){ 
    return name; 
} 

@Override 
public String toString(){ 
    return state + " " + name + " " + type + " " + value; 
} 

} 
+4

'ArrayList '을 확장하지 않아야합니까? 관계없이, 당신은 또한 'Property'에 대한 코드를 보여줄 수 없습니까? – Shark

+1

@Shark 사실. 지금 그대로서는 것은 의미가 없습니다. 그러나,'Property'에 대한 명시 적 캐스트는 정의를보고 싶습니다. – jensgram

+1

클래스 선언이 다음과 같이되어서는 안됩니다 :'public class PropertyList extends ArrayList ' T '를'속성 '으로 변경 하시겠습니까? – Shark

답변

2

명시 적 E, T 또는 기타 GenericType 유사한 일반적인 형태로 사용하는 대신 Property 클래스를 참조하는 코드를 리팩토링해야한다. 귀하의 경우 문제는 PropertyGenericType에 유추하여 콘크리트 Property 클래스를 참조하는 것이 아니라 오히려 그것을 그림자 것을 - 당신은 그래서

public class PropertyList<T> extends ArrayList<T> 

를 사용하는 경우와 같은 그것을 만드는이 라인을 따라 뭔가 :

import java.util.ArrayList; 
import java.util.Collection; 

public class PropertyList extends ArrayList<Property> { 

private static final long serialVersionUID = -7854888805136619636L; 

    PropertyList(){ 
     super(); 
    } 

    PropertyList(Collection<Property> c){ 
     super(c); 
    } 

    boolean containsProperty(PropertyList<Property> pl){ 
     Property asdf = (Property) this.get(4); 
     System.out.println(asdf.<can't access "Property" methods>); //mark 
     return false; 
    } 

} 

IDE가 Property 클래스를 가져 오는 것이 불편하므로 잘 작동한다는 것을 알게 될 것입니다.

당신이 당신의 Property 클래스,

PropertyList<T> extends ArrayList<T> 

를 사용하여 제한 할 수 없지만 당신이 지금 가지고있는 다음 똑같은 동작을 얻을 것이다 경우 - 어떤 방법 조회를 의미하지 않습니다/가용성을 T 이후 아무것도 될 수 있으며, (나는 생각한다) defaulted/infered Object이다.

첫 번째 방법은 표시된 행에 getName() 메서드를 표시해야합니다. 실제로 초기 솔루션/시도처럼 그림자 대신 Property 클래스를 확장하는 일반적인 타입 T을 사용

PropertyList<T extends Property> extends ArrayList<T> 

:

진짜 문제 해결사

이있다.

+1

좋은 답변입니다. 후자의 경우, Generic 'T'는 PropertyList extends ArrayList '과 같은 Property로 제한되어 어떤 Property 하위 유형의 비헤이비어도 유지할 수 있습니다. 때로는 매우 유용합니다! –

+0

좋은 추가, :) 감사에 다시 그것을 추가했습니다. – Shark

+0

잘 쉬웠다 ... 고마워! 당신이 실제로 제네릭으로 확장 할 수 있다는 것을 몰랐습니다. – Medardas

관련 문제