项目结构:
运行效果:
=================================================
代码部分:
=================================================
/Templete/src/com/b510/templete/TempleteOfProblem.java
1 /** 2 * 3 */ 4 package com.b510.templete; 5 6 /** 7 * 问题模板 8 * 9 * @author hongten(hongtenzone@foxmail.com) 10 * @date 2013-2-20 11 */ 12 public abstract class TempleteOfProblem { 13 /** 14 * 姓名 15 */ 16 private String name; 17 /** 18 * 第一个问题 19 */ 20 public abstract void firstProblem(); 21 22 /** 23 * 第二个问题 24 */ 25 public abstract void secondProblem(); 26 27 /** 28 * 第三个问题 29 */ 30 public abstract void thirdProblem(); 31 32 /** 33 * 问题模板(类似于试卷) 34 */ 35 public void templeteOfProblems() { 36 firstProblem(); 37 secondProblem(); 38 thirdProblem(); 39 40 System.out.println("====================="); 41 } 42 43 public String getName() { 44 return name; 45 } 46 47 public void setName(String name) { 48 this.name = name; 49 } 50 51 }
/Templete/src/com/b510/templete/extend/FirstPaper.java
1 /** 2 * 3 */ 4 package com.b510.templete.extend; 5 6 import com.b510.templete.TempleteOfProblem; 7 8 /** 9 * 第一张试卷 10 * @author hongten(hongtenzone@foxmail.com) 11 * @date 2013-2-20 12 */ 13 public class FirstPaper extends TempleteOfProblem{ 14 15 public void firstProblem() { 16 System.out.println(getName()+"第一题选择:A"); 17 } 18 19 public void secondProblem() { 20 System.out.println(getName()+"第二题选择:B"); 21 } 22 23 public void thirdProblem() { 24 System.out.println(getName()+"第三题选择:A"); 25 } 26 27 }
/Templete/src/com/b510/templete/extend/SecondPaper.java
1 /** 2 * 3 */ 4 package com.b510.templete.extend; 5 6 import com.b510.templete.TempleteOfProblem; 7 8 /** 9 * @author hongten(hongtenzone@foxmail.com) 10 * @date 2013-2-20 11 */ 12 public class SecondPaper extends TempleteOfProblem{ 13 14 public void firstProblem() { 15 System.out.println(getName()+"第一题选择:C"); 16 } 17 18 public void secondProblem() { 19 System.out.println(getName()+"第二题选择:D"); 20 } 21 22 public void thirdProblem() { 23 System.out.println(getName()+"第三题选择:B"); 24 } 25 }
/Templete/src/com/b510/templete/extend/ThirdPaper.java
1 /** 2 * 3 */ 4 package com.b510.templete.extend; 5 6 import com.b510.templete.TempleteOfProblem; 7 8 /** 9 * @author hongten(hongtenzone@foxmail.com) 10 * @date 2013-2-20 11 */ 12 public class ThirdPaper extends TempleteOfProblem{ 13 14 public void firstProblem() { 15 System.out.println(getName()+"第一题选择:B"); 16 } 17 18 public void secondProblem() { 19 System.out.println(getName()+"第二题选择:C"); 20 } 21 22 public void thirdProblem() { 23 System.out.println(getName()+"第三题选择:D"); 24 } 25 }
/Templete/src/com/b510/templete/test/TemplateTest.java
1 /** 2 * 3 */ 4 package com.b510.templete.test; 5 6 import com.b510.templete.TempleteOfProblem; 7 import com.b510.templete.extend.FirstPaper; 8 import com.b510.templete.extend.SecondPaper; 9 import com.b510.templete.extend.ThirdPaper; 10 11 /** 12 * 模板测试类(有三个学生,做同一套试题,然而他们各自的答案是不同的) 13 * @author hongten(hongtenzone@foxmail.com) 14 * @date 2013-2-20 15 */ 16 public class TemplateTest { 17 18 public static void main(String[] args) { 19 TempleteOfProblem student = new FirstPaper(); 20 student.setName("wanjin"); 21 student.templeteOfProblems(); 22 23 student = new SecondPaper(); 24 student.setName("hongten"); 25 student.templeteOfProblems(); 26 27 student = new ThirdPaper(); 28 student.setName("hanyuan"); 29 student.templeteOfProblems(); 30 31 } 32 }
哈哈,是不是很简单....