Void와 매개 변수 없음의 차이점은 무엇입니까?
두 개의 오버로드 된 메서드를 정의하는 클래스가 있습니다.
public void handle(Void e)
protected void handle()
분명히 그들은 특히, 다른 handle(Void e)
입니다 public
.
그 둘의 차이점은 무엇입니까?
첫 번째 메서드를 호출하는 방법은 무엇입니까? 사용하고 있습니다 handle(null)
-이것이 맞습니까?
첫 번째 함수는 단일 인수의 함수이며 제공되어야하며 값을 유효하게 만받을 수 있습니다 null
. null 이외의 값은 컴파일되지 않습니다. 두 번째 함수는 인수를 취하지 않으며 전달 null
하면 컴파일되지 않습니다.
Void
일반적으로 리플렉션에만 사용되는 특수 클래스입니다. 주된 용도는 void 메서드의 반환 유형을 나타내는 것입니다. 에 대한 javadoc에서Void
:
Void 클래스는 Java 키워드 void를 나타내는 Class 객체에 대한 참조를 보유하는 인스턴스화 할 수없는 플레이스 홀더 클래스입니다.
Void
클래스를 인스턴스화 할 수 없기 때문에 Void
, 같은 형식 매개 변수 를 사용하여 메서드에 전달할 수있는 유일한 값 handle(Void e)
은 null
입니다.
이것은 공식적인 이벤트 버전이지만 관심이있는 사람들을 위해 javadoc의 반대 주장에도 불구하고 실제로 다음 인스턴스를 인스턴스화 Void
할 수 있습니다Void
.
Constructor<Void> c = Void.class.getDeclaredConstructor();
c.setAccessible(true);
Void v = c.newInstance(); // Hello sailor!
즉, Void
유형이 "무시"되고 있음을 나타낼 때 일반 매개 변수 유형으로 "유용하게"사용되는 것을 보았습니다 . 예를 들면 다음과 같습니다.
Callable<Void> ignoreResult = new Callable<Void> () {
public Void call() throws Exception {
// do something
return null; // only possible value for a Void type
}
}
Callable
의 제네릭 매개 변수는 반환 유형이므로 Void
이와 같이 Callable
사용하면 Executor
프레임 워크를 사용하는 경우 와 같이 인터페이스 사용이 필요 하더라도 반환 된 값이 중요하지 않다는 코드 독자에게 분명한 신호 입니다.
에 API 고려 AsyncTask<T1, T2, T3>
에서 안드로이드 시스템 고리 3 개를 제공합니다 :
class AsyncTask<S, T, V> {
void doInBackground(S...);
void onProgressUpdate(T...);
void onPostExecute(V);
}
제네릭 유형을 확장 할 때 진행률 및 결과 후크에 AsyncTask<T1, T2, T3>
대한 매개 변수 사용에 관심이 없을 수 있으므로 구현은 다음과 같습니다.
class HTTPDownloader extends AsyncTask<URL, Void, Void> {
void doInBackground(URL... urls) {}
void onProgressUpdate(Void... unused) {}
void onPostExecute(Void unused) {}
}
인스턴스화 할 수 없기 null
때문에 매개 변수를 사용하여 이러한 메소드를 호출 할 수 있습니다 Void
.
If Void
isn't actually an instantiation of a type parameter (where it obviously makes sense), there is also sense in declaring a handle(Void)
if your handle
method is subject to an extralinguistic contract that says that the object that wants to participate in a certain protocol must implement a one-argument handle method, regardless of the actual argument type. Now, there may be a special-case implementation that can't handle anything but null
so it makes sense to declare handle(Void)
for such an implementation.
참고URL : https://stackoverflow.com/questions/14030337/whats-the-difference-between-void-and-no-parameter
'Nice programing' 카테고리의 다른 글
여러 if와 elif의 차이점은 무엇입니까? (0) | 2020.12.07 |
---|---|
Javascript를 편집하는 동안 Eclipse 오류가 지속적으로 나타납니다. (0) | 2020.12.07 |
작업 표시 줄 제목 및 부제 설정 (0) | 2020.12.07 |
Angular $ location.path가 작동하지 않습니다. (0) | 2020.12.07 |
Excel VBA에서 구현을 사용하는 방법 (0) | 2020.12.07 |