程序员

Java编程思想读书笔记第九章:接口

2018-08-22  本文已影响11人  刺雒

抽象类和抽象方法

接口

完全解耦

使用类的耦合性
public class Bird {
    public void fly() {
        System.out.println("Bird can fly");
    }
}

class Eagle extends Bird {
    @Override
    public void fly() {
        System.out.println("Eagle can fly");
    }
}

class Swallow extends Bird {
    @Override
    public void fly() {
        System.out.println("Swallow can fly");
    }
}

class Apply {
    public static void fly(Bird bird) {
        bird.fly();
    }
}
public class AirPlane {
    public void fly() {
        System.out.println("AirPlane can fly");
    }
}

class Helicopter extends AirPlane {
    @Override
    public void fly() {
        System.out.println("Helicopter can fly");
    }
}

class Fighter extends AirPlane {
    @Override
    public void fly() {
        System.out.println("Fighter can fly");
    }
}
使用接口解耦
public interface Fly  {
    void fly();
}

public class Bird implements Fly {
    @Override
    public void fly() {
        System.out.println("Bird can fly");
    }
}

public class AirPlane implements Fly {
    @Override
    public void fly() {
        System.out.println("AirPlane can fly");
    }
}

public class Apply {
    public static void fly(Fly fly) {
        fly.fly();
    }
}
适配接口
public class AirPlaneAdapter implements Fly {
    private AirPlane airPlane;

    public AirPlaneAdapter(AirPlane airPlane) {
        this.airPlane = airPlane;
    }

    @Override
    public void fly() {
        airPlane.fly();
    }
}

Apply.fly(new AirPlaneAdapter(new AirPlane()));
困惑

实现多个接口

interface CanFight {
  void fight();
}

interface CanSwim {
  void swim();
}

interface CanFly {
  void fly();
}

class SuperHero implements CanFight, CanSwim, CanFly {
  public void swim() {}
  public void fly() {}
  public void fight(){}
}

public class Adventure {
  public static void t(CanFight x) { x.fight(); }
  public static void u(CanSwim x) { x.swim(); }
  public static void v(CanFly x) { x.fly(); }
  public static void w(ActionCharacter x) { x.fight(); }
  public static void main(String[] args) {
    Hero h = new Hero();
    t(h); // Treat it as a CanFight
    u(h); // Treat it as a CanSwim
    v(h); // Treat it as a CanFly
    w(h); // Treat it as an ActionCharacter
  }
}

类和接口

public class People implements Eat {
    @Override
    public void eat() {
        System.out.println("I can eat rice");
    }
}

public class Woman extends People {
}

public class Man extends People {
}

interface Eat {
    void eat();
}

通过继承扩展接口

接口之间也可以继承,并且可以多继承,也就是说一个接口可以继承多个其他接口,扩展原来的接口。比如原来已经有SwimFight接口,这时候我还想增加一个Help接口也具备游泳和打坏蛋的行为,就可以继承上面两个接口,当SuperHero实现Help接口的时候就需要实现上述3个方法。

interface Swim {
    void canSwim();
}

interface Fight {
    void canFight();
}

interface Help extends Swim, Fight {
    void canHelp();
}

public class SuperHero implements  Help {
    @Override
    public void canSwim() {

    }

    @Override
    public void canFight() {

    }

    @Override
    public void canHelp() {

    }
}

接口与工厂模式

interface Car {
    void running();
}

class Jeep implements Car {
    @Override
    public void running() {
        System.out.println("Jeep's speed is 80km/h");
    }
}

class Track implements Car {
    @Override
    public void running() {
        System.out.println("Track's speed is 60km/h");
    }
}

interface CarFactory {
    Car getCar();
}

class JeepFactory implements CarFactory {
    @Override
    public Car getCar() {
        return new Jeep();
    }
}

class TrackFactory implements CarFactory {
    @Override
    public Car getCar() {
        return new Track();
    }
}

public class User {
    private void drive(CarFactory carFactory) {
        carFactory.getCar().running();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读