무계 와일드카드 - [OOP & Java#10]
12172 단어 computersciencecodenewbiejava
![](https://s1.md5.ltd/image/c35214029e0b846085bed8c55c676bb1.jpg)
경계형 유형 및 와일드카드 - [OOP 및 Java#6]
유영량・ 20년 9월 22일・ 8분 읽기
#computerscience
#java
#oop
비록 나는 범형과 어댑터의 대부분 기초 지식을 소개했지만, 여전히 일부 분야는 더욱 주목하고 토론해야 한다.본문에서 나는 무계 어댑터에 관한 미묘한 점을 쓰고 싶다.
동기
이 주제를 둘러싼 일반적인 질문은 다음과 같습니다.
List<?>
과List<Object>
사이에는 어떤 차이가 있습니까?List<?>
과List<? extends Object>
사이에는 어떤 차이가 있습니까??
또는 List<?>
?p, 비록 나는 예시에서
List<?>
를 사용했지만, 나는 모든 가능한 범주 유형, 예를 들어 ImmutableList<?>
이라는 사용자 정의 범주 유형을 가리킨다.문제 1
What is the difference between
List<?>
andList<Object>
?
우리가 어댑터와 범주를 이야기할 때, 우리의 중점은 항상 유형과 유형이 안전하다.이러한 실현을 사용하는 목적은 다태성을 허용하는 것이다.
우리가 어댑터가 있을 때, 우리는'any'라는 단어를 생각할 수 있다.
List<?>
는 가능한 모든 종류의 목록입니다.유형은 알 수 없지만 어떤 유형이든 바꿀 수 있고 표현식이 유효하다는 것을 알고 있습니다.?
의 상황은 그렇지 않다.불변성으로 인해 단편적인 유형의 관계는 범용에 적용되지 않는다.// This works
Number[] myArr = new Integer[]();
// This does not work
List<Number> myDemo = new List<Integer>();
매개변수 선언에 나타나면 다음과 같은 뚜렷한 차이가 있습니다.// example method declaration
void test(ArrayList<?> myList) {}
// example method declaration
void test2(ArrayList<Object> myList) {}
// this works
test(new ArrayList<Integer>());
// this works
test(new ArrayList<Object>());
// this does not work
test2(new ArrayList<Integer>());
// this works
test2(new ArrayList<Object>());
List<Object>
은 모든 종류의 목록을 받아들일 수 있다test
유형test2
만 받아들일 수 있는 목록Object
다음과 같은 경우에 유용할 수 있습니다.List<?>
정적 방법: Object
또는 List
.문제 2
What is the difference between
List<?>
andList<? extends Object>
?
내가 무계 어댑터를 연구한 후에야 나는 비로소 상술한 상황을 깨달았다.
void test(ArrayList<?> myList) {}
void test2(ArrayList<? extends Object> myList) {}
// this works
test(new ArrayList<Integer>());
// this works
test(new ArrayList<Object>());
// this works
test2(new ArrayList<Integer>());
// this works
test2(new ArrayList<Object>());
기사 Java Generics 에서 언급한 차이점은 다음과 같습니다.Reifiable types are those whose type is not erased at compile time. In other words, a non-reifiable type's runtime representation will have less information than its compile-time counterpart, because some of it'll get erased. The only exception to this rule is unbounded wildcard types.
범용 유형은 실행할 때 삭제됩니다.이것은
List.size
이 번역된 후에 원시 유형List.clear
이 되었다는 것을 의미한다.단, 번역 후 List<Integer>
는 List
로 유지됩니다.만약 우리가 범용 유형에 대한 정보를 가지고 있다면, 우리는 그것에 대해 응용할 수 있다List<?>
.그래서
List<?>
복구 불가능, 실행 중 instanceof
으로 변경됩니다.List<? extends Object>
는 복구 가능하며 운행 시 List
로 변경됩니다.// this compiles
// allowed to use instanceof on unbounded wildcards
List aList = new ArrayList<Integer>();
aList instanceof List<?>;
// this does not compile
// not allowed to use instanceof on bounded wildcards
aList instanceof List<? extends Object>;
나는 이 차이가 현저하지 않다고 생각하지만, 더 많이 알아도 무방하다:)질문
Can return types be
?
orList<?>
?
갑골문Java tutorial에 따르면
Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards.
그래서 가능하지만 추천하지 않습니다.실제로 반환 유형
List<?>
또는 List<?>
에 문제가 있습니다.?
List<?>
로 선언하면 컴파일 오류가 발생합니다.test.java:27: error: illegal start of type
public ? test() {
error: invalid method declaration; return type required
public ? test() {
?
// method declaration
public List<?> test() { return new ArrayList<Integer>();}
// this does not work
// error: incompatible types: java.util.List<capture#1 of ?>
// cannot be converted to java.util.List<java.lang.Integer>
List<Integer> ls = test();
// this works but may be of little use
List<?> ls = test(); // does compile
반환된 객체가 실제로는 ?
인 것을 알고 있더라도 List<?>
는 특정 유형(예: List<Integer>
에 할당할 수 없습니다.따라서 목록의 항목에 대해 List<?>
관련 방법을 사용할 수 없습니다.우리는 일반적인 어댑터를 블랙박스로 사용할 수 있는 장점을 얻었지만, 그 후에 우리는 그것을 조작할 능력을 잃었다.참고
Reference
이 문제에 관하여(무계 와일드카드 - [OOP & Java#10]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tlylt/unbounded-wildcards-oop-java-10-213f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)