集册 Java实例教程 在不破坏现有代码的情况下修改接口

在不破坏现有代码的情况下修改接口

欢马劈雪     最近更新时间:2020-01-02 10:19:05

613
用默认方法创建一个接口,任何实现该接口的类都可以使用该方法。

import java.util.List;


interface TeamType { /*来 自 NowJava.com - 时代Java*/


    List<Player> getPlayers(); 


    void setPlayers(List<Player> players); 


    void setName(String name); 


    void setCity(String city); 


    String getFullName(); 
/* 来自 时 代 J a v a 公 众 号*/

    default void listPlayers() { 

        getPlayers().stream().forEach((player) -> { 

            System.out.println(player.getFirstName() + " " + player.getLastName()); 

        }); 

    } 


} 

class Player {

  

  private String firstName;

  private String lastName;

  private String position;

  private int status = -1;

  

  public Player(){

      

  }

  

  public Player(String position, int status){

      this.position = position;

      this.status = status;

  }

  


  public String playerString(){

      return getFirstName() + " " + getLastName() + " - " + getPosition();

  }


  /**

   * @return the firstName

   */

  public String getFirstName() {

      return firstName;

  }


  /**

   * @param firstName the firstName to set

   */

  public void setFirstName(String firstName) {

      this.firstName = firstName;

  }


  /**

   * @return the lastName

   */

  public String getLastName() {

      return lastName;

  }


  /**

   * @param lastName the lastName to set

   */

  public void setLastName(String lastName) {

      this.lastName = lastName;

  }


  /**

   * @return the position

   */

  public String getPosition() {

      return position;

  }


  /**

   * @param position the position to set

   */

  public void setPosition(String position) {

      
展开阅读全文