练习11:(3)修改 Detergent.java,让它使用代理。
一:我的答案:
1 package com.learnJava.test; 2 3 /** 4 * @Author zhuchangli 5 * @Date 2019/9/15 6 **/ 7 public class DetergentDelegation { 8 9 private Cleanser cleanser = new Cleanser(); 10 private String s = "Cleanser "; 11 public void scrub(){ 12 cleanser.append(" Detergent.scrub()"); 13 cleanser.scrub(); 14 } 15 16 public void append(String a){ 17 cleanser.append(a); 18 } 19 20 public void dilute(){ 21 cleanser.dilute(); 22 } 23 24 public void apply(){ 25 cleanser.applay(); 26 } 27 28 public void foam(){ 29 append("foam()"); 30 } 31 32 public String toString(){ 33 return cleanser.toString(); 34 } 35 36 public static void main(String [] args){ 37 DetergentDelegation d = new DetergentDelegation(); 38 d.dilute(); 39 d.apply(); 40 d.scrub(); 41 d.foam(); 42 System.out.println(d); 43 System.out.println("Testing base class"); 44 Cleanser.main(args); 45 46 } 47 }
运行结果:
Cleanserdilute()applay() Detergent.scrub()scrub()foam()
Testing base class
Cleanserdilute()applay()scrub()
使用继承时:
1 package com.learnJava.test; 2 3 /** 4 * @Author zhuchangli 5 * @Date 2019/9/14 6 **/ 7 class Cleanser{ 8 private String s = "Cleanser"; 9 public void append(String a) { s += a; } 10 public void dilute(){ append("dilute()"); } 11 public void applay(){append("applay()");} 12 public void scrub(){append("scrub()");} 13 public String toString(){return s;} 14 15 public static void main(String [] args){ 16 Cleanser x = new Cleanser(); 17 x.dilute(); 18 x.applay(); 19 x.scrub(); 20 System.out.println(x); 21 } 22 } 23 //洗涤剂 24 public class Detergent extends Cleanser{ 25 public void scrub(){ 26 append("Detergent.scrub()"); 27 super.scrub(); 28 } 29 30 // foam 泡沫 31 public void foam(){ append("foam()");} 32 33 public static void main(String [] args){ 34 Detergent x = new Detergent(); 35 x.dilute(); 36 x.applay(); 37 x.scrub(); 38 x.foam(); 39 System.out.println(x); 40 System.out.println("Testing base class"); 41 Cleanser.main(args); 42 } 43 }
运行结果:
Cleanserdilute()applay()Detergent.scrub()scrub()foam()
Testing base class
Cleanserdilute()applay()scrub()
二:参考答案
1 package com.learnJava.test; 2 3 /** 4 * @Author zhuchangli 5 * @Date 2019/9/15 6 **/ 7 8 class Cleanser11{ 9 private String s = "Cleanser11"; 10 public void append(String a) { s += a;} 11 public void dilute() {append(" dilute()");} 12 public void apply() {append(" apply()");} 13 public void scrub() {append(" scrub()");} 14 public String toString() {return s;} 15 16 public static void main(String [] args){ 17 Cleanser11 x = new Cleanser11(); 18 x.dilute(); 19 x.apply(); 20 x.scrub(); 21 System.out.println(x); 22 } 23 } 24 25 public class DetergentDelegation11 { 26 27 private String s = "DetergentDelegation11 "; 28 private Cleanser11 c11 = new Cleanser11(); 29 30 public void append(String a){ 31 s += a; 32 } 33 34 public void dilute(){ 35 c11.dilute(); 36 } 37 38 public void apply() {c11.apply();} 39 40 public void scrub() { 41 append("DetergentDelegation11"); 42 c11.scrub(); 43 } 44 45 public void foam(){ 46 append(" foam()"); 47 } 48 49 public String toString(){ 50 return s + " " + c11; 51 } 52 53 public static void main(String [] args){ 54 DetergentDelegation11 d = new DetergentDelegation11(); 55 d.dilute(); 56 d.apply(); 57 d.scrub(); 58 d.foam(); 59 System.out.println(d); 60 System.out.println("Testing base class"); 61 Cleanser11.main(args); 62 } 63 64 65 }
运行结果:
DetergentDelegation11 DetergentDelegation11 foam() Cleanser11 dilute() apply() scrub()
Testing base class
Cleanser11 dilute() apply() scrub()
总结:
代理是什么:
将一个成员置于所要构造的类中,在新类中暴露了该成员对象的所有方法。
只提供在成员对象的方法的某个子集。