support multiple-inheritance. Following example can achieve multiple-inheritance by implementing an
adapter design pattern.
package pattern;
public interface Dog {
public String getDogSound();
}
package pattern;
public interface Cat {
public String getCatSound();
}
package pattern;
public class AnimalAdapter implements Cat,Dog {
@Override
public String getCatSound() {
return "Cat sound";
}
@Override
public String getDogSound() {
return "Dog sound";
}
}
AnimalAdapter have both cat's voice and dog's voice.
package pattern;
public class Simulator {
static boolean cat = true;
public static void main(String[]args){
AnimalAdapter aa = new AnimalAdapter();
if(cat){
System.out.println(aa.getCatSound());
}else if(!cat){
System.out.println(aa.getDogSound());
}
}
}
Therefore , simulator can produce cat voice or dog voice in different situations.Reference:Wiki Article
沒有留言:
張貼留言