集册 Java实例教程 通过接口与类交互

通过接口与类交互

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

638
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过接口与类交互

import java.util.List;


public class Main {/**来自 n  o  w  j  a  v  a . c o m**/


  static TeamType team = new Team();


  public static void main(String[] args) {

    team.setCity("SomeCity");

    team.setName("SomeName");

    team.setPlayers(null);

    System.out.println(team.getFullName());

  }


}


interface TeamType {
/*
来 自*
 时 代 J a v a
*/


  void setPlayers(List<Player> players);


  void setName(String name);


  void setCity(String city);


  String getFullName();


}


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) {

    this.position = position;

  }


  /**

   * @return the status

   */

  public int getStatus() {

    return status;

  }


  /**

   * @param status

   *          the status to set

   */

  public void setStatus(int status) {

    this.status = status;

  }

}


class Team implements TeamType {


  private List<Player> players;

  private String name;

  private String city;


  /**

   * @return the players

   */

  public List<Player> getPlayers() {

    return players;

  }


  /**

   * @param players

   *          the players to set

   */

  public void setPlayers(List<Player> players) {

    this.players = players;

  }


  /**

   * @return the name

   */

  public String getName() {

    return name;

  }


  
展开阅读全文