摘自:《大話設計模式》java
學生張三抄的試卷設計模式
public class ZhangSanPaper { public void test1(){ //試題1 System.out.println("不屬於java關鍵字: A.int B.user C.static D.public"); System.out.println("答案:b"); } public void test2(){ //試題2 System.out.println("console.log(false == '0');console.log(false === '0');console.log(false == 'false');輸出結果爲 " + " A.true false fasle B.false fasle true C.false true fasle D.flale false fasle"); System.out.println("答案:a"); } public void test3(){ //試題3 System.out.println("使用Java語言編寫的源程序保存時的文件擴展名是( )。(A).class (B).java (C).cpp (D).txt"); System.out.println("答案:b"); } }
李四抄的試卷ide
public class LiSiPaper { public void test1(){ //試題1 System.out.println("不屬於java關鍵字: A.int B.user C.static D.public"); System.out.println("答案:c"); } public void test2(){ //試題2 System.out.println("console.log(false == '0');console.log(false === '0');console.log(false == 'false');輸出結果爲 " + " A.true false fasle B.false fasle true C.false true fasle D.flale false fasle"); System.out.println("答案:b"); } public void test3(){ //試題3 System.out.println("使用Java語言編寫的源程序保存時的文件擴展名是( )。(A).class (B).java (C).cpp (D).txt"); System.out.println("答案:a"); } }
測試測試
package moban.shejimoshi.test02; public class Test { public static void main(String[] args) { System.out.println("學生張三抄的試卷"); ZhangSanPaper zhangSanPaper = new ZhangSanPaper(); zhangSanPaper.test1(); zhangSanPaper.test2(); zhangSanPaper.test3(); System.out.println("======================================="); System.out.println("學生李四抄的試卷"); LiSiPaper liSiPaper = new LiSiPaper(); liSiPaper.test1(); liSiPaper.test2(); liSiPaper.test3(); } }
試卷父類代碼spa
public class TestPaper { public void test1(){ //試題1 System.out.println("不屬於java關鍵字: A.int B.user C.static D.public"); } public void test2(){ //試題2 System.out.println("console.log(false == '0');console.log(false === '0');console.log(false == 'false');輸出結果爲 " + " A.true false fasle B.false fasle true C.false true fasle D.flale false fasle"); } public void test3(){ //試題3 System.out.println("使用Java語言編寫的源程序保存時的文件擴展名是( )。(A).class (B).java (C).cpp (D).txt"); } }
學生張三抄的試卷.net
package moban.shejimoshi.test03; public class ZhangSanPaper extends TestPaper{ @Override public void test1(){ //試題1 super.test1(); System.out.println("答案:b"); } @Override public void test2(){ //試題2 super.test2(); System.out.println("答案:a"); } @Override public void test3(){ //試題3 super.test3(); System.out.println("答案:a"); } }
李四抄的試卷設計
package moban.shejimoshi.test03; public class LiSiPaper extends TestPaper{ @Override public void test1(){ //試題1 super.test1(); System.out.println("答案:c"); } @Override public void test2(){ //試題2 super.test2(); System.out.println("答案:b"); } @Override public void test3(){ //試題3 super.test3(); System.out.println("答案:c"); } }
測試代碼略code
父類代碼blog
package moban.shejimoshi; public class TestPaper { public void test1(){ //試題1 System.out.println("不屬於java關鍵字: A.int B.user C.static D.public"); System.out.println("答案:"+answer1()); } public void test2(){ //試題2 System.out.println("console.log(false == '0');console.log(false === '0');console.log(false == 'false');輸出結果爲 " + " A.true false fasle B.false fasle true C.false true fasle D.flale false fasle"); System.out.println("答案:"+answer2()); } public void test3(){ //試題3 System.out.println("使用Java語言編寫的源程序保存時的文件擴展名是( )。(A).class (B).java (C).cpp (D).txt 2"); System.out.println("答案:"+answer3()); } public String answer1() { return ""; } public String answer2() { return ""; } public String answer3() { return ""; } }
張三get
public class ZhangSanPaper extends TestPaper { @Override public String answer1() { return "a"; } @Override public String answer2() { return "b"; } @Override public String answer3() { return "b"; } }
李四
public class LiSiPaper extends TestPaper { @Override public String answer1() { return "b"; } @Override public String answer2() { return "a"; } @Override public String answer3() { return "d"; } }
測試代碼略
UML圖
下一篇更精彩: Java設計模式之模板模式(Template )第二篇