开源中文网

您的位置: 首页 > 编程开发 > Java语言设计 > 正文

java接口实例2 -- 强制执行特定的方法

来源:  作者:

Java的接口用法之一,暴露特定的方法给类使用;用法之二,当某个事件发生的时候,强制执行特定的方法。

 
举例说明:
假设有一个接口,监听足球比赛的分数,当分数发生变化的时候,将当前的比赛成绩公布到一个电子版上。
 

package cn.youhap.football;

public interface FootballListener {
    public void setHomeTeam(String h);
    public void setVisitingTeam(String v);
    public void setEndOfQuarter(int q);
    public void homeTeamScored();
    public void visitingTeamScored();
}


足球比赛的类:
 

package cn.youhap.football;

import java.util.Vector;

public class FootballGame {
    private String visitingTeam;
    private String homeTeam;
    public static int visitingTeamScore = 0;
    public static int homeTeamScore = 0;
    private int endOfQuarter;
    private Vector<FootballListener> audious;
    public FootballGame(String h, String v, int q){
        this.setHomeTeam(h);
        this.setVisitingTeam(v);
        this.setEndOfQuarter(q);
        this.audious = new Vector<FootballListener>(10);
    }
    public void setVisitingTeam(String visitingTeam) {
        this.visitingTeam = visitingTeam;
    }
    public String getVisitingTeam() {
        return visitingTeam;
    }
    public void setHomeTeam(String homeTeam) {
        this.homeTeam = homeTeam;
    }
    public String getHomeTeam() {
        return homeTeam;
    }

    public int getVisitingTeamScore() {
        return visitingTeamScore;
    }

    public int getHomeTeamScore() {
        return homeTeamScore;
    }
    public void setEndOfQuarter(int endOfQuarter) {
        this.endOfQuarter = endOfQuarter;
    }
    public int getEndOfQuarter() {
        return endOfQuarter;
    }
    public void homeTeamScored(){
        int audiousSize = this.audious.size();
        for (int i = 0; i < audiousSize; i++){
            FootballListener current = (FootballListener)this.audious.elementAt(i);
            current.homeTeamScored();
        }
    }
    public void visitingTeamScored(){
        int audiousSize = this.audious.size();
        for (int i = 0; i < audiousSize; i++){
            FootballListener current = (FootballListener)this.audious.elementAt(i);
            current.visitingTeamScored();
        }
    }
    public void addListener(FootballListener f){
        this.audious.add(f);
        f.setHomeTeam(this.getHomeTeam());
        f.setVisitingTeam(this.getVisitingTeam());
    }
    public void endOfQuarter(int quarter){
        int audiousSize = this.audious.size();
        for (int i = 0; i < audiousSize; i++){
            FootballListener current = (FootballListener)this.audious.elementAt(i);
            current.setEndOfQuarter(quarter);
        }
    }
    
}

 


当homeTeamScored(主队得分)visitingTeamScored(客队得分)的事件发生的时候,将电子版等“听众”加入到向量中,逐个通知它们比赛成绩。
 
由于电子版实现了接口FootballListener,根据多态,将执行电子版的homeTeamScored或者visitingTeamScored方法。
 
 

package cn.youhap.football;

public class ScoreBoard implements FootballListener {
    private String homeTeam;
    private String visitingTeam;
    private int quarter;
    private int homeTeamScore;
    private int visitingTeamScore;
    public ScoreBoard(){
        super();
    }
    public void setHomeTeam(String h){
        this.homeTeam = h;
    }
    public String getHomeTeam(){
        return this.homeTeam;
    }
    public void setVisitingTeam(String v){
        this.visitingTeam = v;
    }
    public String getVisitingTeam() {
        return this.visitingTeam;
    }
    public void setEndOfQuarter(int q){
        this.quarter = q;
    }
    public void setHomeTeamScore(int homeTeamScore) {
        this.homeTeamScore = homeTeamScore;
    }
    public int getHomeTeamScore() {
        return homeTeamScore;
    }
    public void setVisitingTeamScore(int visitingTeamScore) {
        this.visitingTeamScore = visitingTeamScore;
    }
    public int getVisitingTeamScore() {
        return visitingTeamScore;
    }
    public void homeTeamScored(){
        this.homeTeamScore++;
        this.displayScore();
    }
    public void visitingTeamScored(){
        this.visitingTeamScore++;
        this.displayScore();
    }
    public int getEndOfQuarter(){
        return this.quarter;
    }
    public void displayScore(){
        System.out.println("***************************************************");
        System.out.println(this.getHomeTeam() + ":" + this.getHomeTeamScore());
        System.out.println(this.getVisitingTeam() + ":" + this.getVisitingTeamScore());
        System.out.println("***************************************************");
    }

}


演示:
 

package cn.youhap.football;

public class FootballDemo {
    public static void main(String[] args) {
        FootballGame football = new FootballGame("巴西", "德国", 1);
        ScoreBoard s = new ScoreBoard();
        football.addListener(s);
        football.homeTeamScored();
        football.visitingTeamScored();
        football.homeTeamScored();
    }
}


结果:
***************************************************
巴西:1
德国:0
***************************************************
***************************************************
巴西:1
德国:1
***************************************************
***************************************************
巴西:2
德国:1
***************************************************

Tags:实例 接口 方法
关于开源中文网 - 联系我们 - 广告服务 - 网站地图 - 版权声明