1 public class Student 2 { 3 private String name = null; 4 5 public Student(String name) 6 { 7 // TODO Auto-generated constructor stub 8 this.name = name; 9 } 10 11 public void setName(String name) 12 { 13 this.name = name; 14 } 15 16 private int calcADD(int a, int b) 17 { 18 return a + b; 19 } 20 21 public void fillBlank(int a, int b) 22 { 23 int result = calcADD(a, b); 24 System.out.println(name + "心算:" + a + " + " + b + " = " + result); 25 } 26 }
客户端代码:
1 2 3 4 5 6 7 8 9 10
1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int a = 1; 6 int b = 1; 7 Student s = new Student("小明"); 8 s.fillBlank(a, b); 9 } 10 }
1 public class Student 2 { 3 private String name = null; 4 5 public Student(String name) 6 { 7 // TODO Auto-generated constructor stub 8 this.name = name; 9 } 10 11 public void setName(String name) 12 { 13 this.name = name; 14 } 15 16 @SuppressWarnings("unused") 17 private int calcADD(int a, int b) 18 { 19 return a + b; 20 } 21 22 private int useCalculator(int a, int b) 23 { 24 return new Calculator().add(a, b); 25 } 26 27 public void fillBlank(int a, int b) 28 { 29 int result = useCalculator(a, b); 30 System.out.println(name + "使用计算器:" + a + " + " + b + " = " + result); 31 } 32 }
如上所示。客户端代码如下:
1 2 3 4 5 6 7 8 9 10
1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int a = 189; 6 int b = 231; 7 Student s = new Student("小明"); 8 s.fillBlank(a, b); 9 } 10 }
public class SuperCalculator 2 { 3 public void add(int a, int b, Student xiaoming) 4 { 5 int result = a + b; 6 xiaoming.fillBlank(a, b, result); 7 } 8 }
public class Student { private String name = null;
public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, this); } public void fillBlank(int a, int b, int result) { System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result); } }
客户端代码:
1 2 3 4 5 6 7 8 9 10
public class Test { public static void main(String[] args) { int a = 26549; int b = 16487; Student s = new Student("小明"); s.callHelp(a, b); } }
public class Student { private String name = null;
public Student(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public class doHomeWork implements doJob {
@Override public void fillBlank(int a, int b, int result) { // TODO Auto-generated method stub System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result); } } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, new doHomeWork()); } }
public Seller(String name) { // TODO Auto-generated constructor stub this.name = name; } public void setName(String name) { this.name = name; } public class doHomeWork implements doJob {
@Override public void fillBlank(int a, int b, int result) { // TODO Auto-generated method stub System.out.println(name + "求助小红算账:" + a + " + " + b + " = " + result + "元"); } } public void callHelp (int a, int b) { new SuperCalculator().add(a, b, new doHomeWork()); } }
客户端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class Test { public static void main(String[] args) { int a = 56; int b = 31; int c = 26497; int d = 11256; Student s1 = new Student("小明"); Seller s2 = new Seller("老婆婆"); s1.callHelp(a, b); s2.callHelp(c, d); } }