类适配器
public class Src220 {
int output(){
return 220;
}
}
public interface Des5 {
int input();
}
public class VolAdapter extends Src220 implements Des5{
@Override
public int input() {
int output = output();
return output/44;
}
}
public class Phone {
void charge(Des5 des){
int input = des.input();
}
}
public class Client {
public static void main(String[] args) {
Phone p = new Phone();
p.charge(new VolAdapter());
}
}
对象适配器
public class Src220 {
int output(){
return 220;
}
}
public interface Des5 {
int input();
}
public class VolAdapter implements Des5{
private Src220 src;
public VolAdapter(Src220 src) {
this.src = src;
}
@Override
public int input() {
int output = src.output();
return output/44;
}
}
public class Phone {
void charge(Des5 des){
int input = des.input();
}
}
public class Client {
public static void main(String[] args) {
Phone p = new Phone();
p.charge(new VolAdapter(new Src220()));
}
}
接口适配器
public interface SrcInterface {
int add();
int subtract();
}
public class AbsAdapter implements SrcInterface{
@Override
public int add() {
return 0;
}
@Override
public int subtract() {
return 0;
}
}
public class Des extends AbsAdapter{
@Override
public int add() {
return super.add();
}
}