난 당신이 혼란 생각, 당신은 그것이 Interface
의 구체적인 구현에서 개체 인 인터페이스
public Main(Interface obj) {
obj.foo();
}
OBJ에게의 인터페이스 타입의 cuase 생각합니다.
당신은 예를 들어
Strategy Pattern
으로이 방법을 몇 가지 일반적인 디자인 패턴을 표시 할 수 있습니다
: 대신 구체적인 구현의 추상적 인 계약에 의해 인도의 주요 장점입니다
public interface Searcher {
void search(String text, List<String> words);
}
public class BinarySearcher implements Searcher{
@Override
public void search(String text , List<String> words){
//code here
}
}
public class LinearSearcher implements Searcher{
@Override
public void search(String text ,List<String> words){
// code here
}
}
public class WordContext {
private Searcher searcher;
private List<String> words;
public void makeSearch(String text){
searcher.search(); // you only know at runtime what subtype will be searcher
}
// here you inject by contract
public void setSearcher(Searcher searcher){
this.searcher= searcher;
}
// here you inject by contract
public void setWords(List<String> words){
this.words = words;
}
}
합니다. 이 예제에서는 주사기를 주사기로 변경할 수 있으며, linearSearcher 또는 binarySearcher가 될 수 있습니다. 즉, 다형성 마법입니다.
왜 호출 할 수 없습니까? –
Inteface obj는 참조 변수 일뿐입니다. 구체적인 구현을 참조하고 있습니다. 그래서 당신이 혼란 스럽습니다. – nachokk