Java 多态 polymorphic 枚举 练习

  

Java 多态 polymorphic 枚举 练习

 

 

package ersatz;

public class Car {
  private double temperature;

  public Car(double temperature) {
    this.temperature = temperature;
  }

  private class Air {
    public void tune() {
      if (temperature >= 40) {
        System.out.println("cold air");
      } else if (temperature < 0) {
        System.out.println("warm air");
      } else {
        System.out.println("nothing");
      }
    }
  }

  public Air getAir() {
    return new Air();
  }

  public static void main(String[] args) {
    final Car car = new Car(50);
    car.getAir().tune();
    final Car car1 = new Car(-5);
    car1.getAir().tune();
    final Car car2 = new Car(33);
    car2.getAir().tune();
  }
}

  

package ersatz;

public class Car {
  private double temperature;

  public Car(double temperature) {
    this.temperature = temperature;
  }

  private class Air {
    public void tune() {
      if (temperature >= 40) {
        System.out.println("cold air");
      } else if (temperature < 0) {
        System.out.println("warm air");
      } else {
        System.out.println("nothing");
      }
    }
  }

  public void regulate() {
    new Air().tune();
  }

  public static void main(String[] args) {
    final Car car = new Car(50);
    car.regulate();
    final Car car1 = new Car(-5);
    car1.regulate();
    final Car car2 = new Car(33);
    car2.regulate();
  }
}

  

 

Java 多态 polymorphic 枚举 练习

 

 

Vehicle.java

package com.gibe;

public interface Vehicle {
  public abstract void work();
}

  

Horse.java

  

package com.gibe;

public class Horse implements Vehicle{
  @Override
  public void work(){
    System.out.println("use horse");
  }
}

  

Boat.java

package com.gibe;

public class Boat implements Vehicle{
  @Override
  public void work(){
    System.out.println("use boat");
  }
}

  

Plane.java

package com.gibe;

public class Plane implements Vehicle{
  @Override
  public void work() {
    System.out.println("use plane");
  }
}

  

VehicleFactory.java

package com.gibe;

public class VehicleFactory {
  private static Horse horse = null;
  private static final Boat boat = new Boat();

  private VehicleFactory() {
  }

  public static Horse getHorse() {
    if (horse == null) {
      horse = new Horse();
    }
    return horse;
  }

  public static Boat getBoat() {
    return boat;
  }

  public static Plane getPlane() {
    return new Plane();
  }
}

  

Person.java

package com.gibe;

public class Person {
  private String name;
  private Vehicle vehicle;

  public Person(String name, Vehicle vehicle) {
    this.name = name;
    this.vehicle = vehicle;
  }

  public void river() {
    if (!(this.vehicle instanceof Boat)) {
      vehicle = VehicleFactory.getBoat();
    }
    vehicle.work();
  }

  public void land() {
    if (!(vehicle instanceof Horse)) {
      vehicle = VehicleFactory.getHorse();
    }
    vehicle.work();
  }

  public void fire() {
    if (!(vehicle instanceof Plane)) {
      vehicle = VehicleFactory.getPlane();
    }
    vehicle.work();
  }
}

  

Test

package ersatz;


import com.gibe.Person;
import com.gibe.VehicleFactory;

public class Ersatz {
  @SuppressWarnings({"SpellCheckingInspection"})
  public static void main(String[] args) {
    final Person bb = new Person("bb", VehicleFactory.getHorse());
    bb.land();
    bb.river();
    bb.fire();
    bb.river();
  }
}

  

Java 多态 polymorphic 枚举 练习

 

 

package com.gibe;

public class Ersatz {
  public static void main(String[] args) {
    Color green = Color.GREEN;
    green.show();
    switch (green) {
      case YELLOW:
        System.out.println("yellow");
        break;
      case BLACK:
        System.out.println("black");
        break;
      default:
        System.out.println("none");
        break;
    }
  }
}


enum Color implements Interface {
  RED(255, 0, 0), BLUE(0, 0, 255),
  BLACK(0, 0, 0), YELLOW(255, 255, 0),
  GREEN(0, 255, 0);
  private final int red;
  private final int green;
  private final int blue;

  private Color(int red, int green, int blue) {
    this.red = red;
    this.green = green;
    this.blue = blue;
  }

  @Override
  public void show() {
    System.out.println("red=" + red + ", green=" + green + ", blue=" + blue);
  }
}

interface Interface {
  public abstract void show();
}

  

 

上一篇:UVa 12661 - Funny Car Racing (最短路变形)


下一篇:【做题记录】CF746F Music in Car