오브젝트를 Java 형식으로 참조 할 수 있습니까?오브젝트 유형별로 참조 Java
private static class Pet {
private String name;
public String getName() {
return name;
}
public Pet setName(String name) {
this.name = name;
return this;
}
}
public static class CAT extends Pet{}
public static class DOG extends Pet{}
나는 애완 동물을 허용하는 연결리스트로 개와 고양이의 애완 동물의 무리를 넣어 :
예를 들어, 나는이 있습니다. 다음을 통해 마지막 색인 인 개를 찾고 싶습니다.
private Pet dequeueDog() {
int locationDog = linkedList.lastIndexOf(DOG);
return linkedList.remove(locationDog);
}
이렇게 할 수 있습니까? 객체의 유형을 기준으로 객체를 참조하려면?
아니요 거꾸로 목록을 반복, 테스트 개체가 해당 유형의 참조의 경우 수 있습니다. –
환상적입니다. 실제로 객체가 유형의 참조인지 테스트 할 수 있는지 물어 보려고했습니다. 답을 써서 이것을 해결할 수 있습니까? getclass() 사용. – lawonga
@lawonga 또는 instanceof. – immibis