--------------------------------CompetitionResult.java-------------------------------------
package com.lch.olympic;
package com.lch.olympic;
/** 比賽結果 **/
public enum CompetitionResult {
WIN {
public String getResult() {
return "升國旗,奏國歌,頒獎!";
}
},
DRAW {
public String getResult() {
return "國旗平置,頒獎!";
}
},
LOSE {
public String getResult() {
return "奧運精神的體現,精神的勝者!";
}
};
abstract String getResult();
}
public enum CompetitionResult {
WIN {
public String getResult() {
return "升國旗,奏國歌,頒獎!";
}
},
DRAW {
public String getResult() {
return "國旗平置,頒獎!";
}
},
LOSE {
public String getResult() {
return "奧運精神的體現,精神的勝者!";
}
};
abstract String getResult();
}
------------------------------------IOlympicComp.java---------------------------------
package com.lch.olympic;
package com.lch.olympic;
/** 比賽結果處理 **/
public interface IOlympicComp {
String winGame();
public interface IOlympicComp {
String winGame();
String drawGame();
String loseGame();
}
}
------------------------------------NormalOlympic.java---------------------------------
package com.lch.olympic;
package com.lch.olympic;
public abstract class NormalOlympic implements IOlympicComp {
public String drawGame() {
return CompetitionResult.DRAW.getResult();
}
return CompetitionResult.DRAW.getResult();
}
public String loseGame() {
return CompetitionResult.LOSE.getResult();
}
return CompetitionResult.LOSE.getResult();
}
public String winGame() {
return CompetitionResult.WIN.getResult();
}
return CompetitionResult.WIN.getResult();
}
}
---------------------------------LondonOlympic.java------------------------------------
package com.lch.olympic;
package com.lch.olympic;
public class LondonOlympic extends NormalOlympic {
protected String nation;
protected String nation;
enum Nation {
CHINA {
public String getNationName() {
return "CHINA";
}
},
OTHERS {
public String getNationName() {
return "OTHERS";
}
};
abstract String getNationName();
}
CHINA {
public String getNationName() {
return "CHINA";
}
},
OTHERS {
public String getNationName() {
return "OTHERS";
}
};
abstract String getNationName();
}
/** 私有化默認構造子 **/
private LondonOlympic() {
}
private LondonOlympic() {
}
/**
* 構造子
*
* @param nation國家
*
* **/
public LondonOlympic(String nation) {
this.nation = nation;
}
* 構造子
*
* @param nation國家
*
* **/
public LondonOlympic(String nation) {
this.nation = nation;
}
@Override
public String drawGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "國旗放其它國家下面!";
} else {
result = CompetitionResult.DRAW.getResult();
}
return result;
}
public String drawGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "國旗放其它國家下面!";
} else {
result = CompetitionResult.DRAW.getResult();
}
return result;
}
@Override
public String loseGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "消極比賽,取消比賽資格!";
} else {
result = CompetitionResult.LOSE.getResult();
}
return result;
}
public String loseGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "消極比賽,取消比賽資格!";
} else {
result = CompetitionResult.LOSE.getResult();
}
return result;
}
@Override
public String winGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "使用興奮劑?!";
} else {
result = CompetitionResult.WIN.getResult();
}
return result;
}
public String winGame() {
String result;
if (Nation.CHINA.getNationName().equals(nation)) {
result = "使用興奮劑?!";
} else {
result = CompetitionResult.WIN.getResult();
}
return result;
}
}
---------------------------------Test.java------------------------------------
package com.lch.olympic;
package com.lch.olympic;
public class Test {
private final String CHINA = "CHINA";
private final String NOT_CHINA = "CDA";
private final String CHINA = "CHINA";
private final String NOT_CHINA = "CDA";
public void comp4China() {
LondonOlympic olympic = new LondonOlympic(CHINA);
System.out.println("中國贏得比賽:" + olympic.winGame());
System.out.println("中國平了比賽:" + olympic.drawGame());
System.out.println("中國輸了比賽:" + olympic.loseGame());
}
LondonOlympic olympic = new LondonOlympic(CHINA);
System.out.println("中國贏得比賽:" + olympic.winGame());
System.out.println("中國平了比賽:" + olympic.drawGame());
System.out.println("中國輸了比賽:" + olympic.loseGame());
}
public void comp4Other() {
LondonOlympic olympic = new LondonOlympic(NOT_CHINA);
System.out.println("非中國贏得比賽:" + olympic.winGame());
System.out.println("非中國平了比賽:" + olympic.drawGame());
System.out.println("非中國輸了比賽:" + olympic.loseGame());
}
LondonOlympic olympic = new LondonOlympic(NOT_CHINA);
System.out.println("非中國贏得比賽:" + olympic.winGame());
System.out.println("非中國平了比賽:" + olympic.drawGame());
System.out.println("非中國輸了比賽:" + olympic.loseGame());
}
public static void main(String[] args) {
Test t = new Test();
System.out.println("\n*********** London Olympic ***********\n");
t.comp4China();
System.out.println("\n=========== 楚河 漢界 ===========\n");
t.comp4Other();
System.out.println("\n*********** London Olympic ***********\n");
}
}
Test t = new Test();
System.out.println("\n*********** London Olympic ***********\n");
t.comp4China();
System.out.println("\n=========== 楚河 漢界 ===========\n");
t.comp4Other();
System.out.println("\n*********** London Olympic ***********\n");
}
}