多态以及 LeetCode 每日一题
1 多态
1.1 多态性
java 引用变量有两个类型:一个是编译时类型,一个是运行时类型。前者是代码中声明这个变量时的类型,后者是由实际对象的类型决定的。当编译类型和运行类型不一样时,产生多态。
1 class baseclass{ 2 public int book = 6; 3 public void base(){ 4 system.out.println("father loves you!"); 5 } 6 public void test(){ 7 system.out.println("father's method is covered"); 8 } 9 } 10 public class subclass extends baseclass{ 11 public string book = "marvel comic book"; 12 public void test(){ 13 system.out.println("son's method covers father's"); 14 } 15 public void sub(){ 16 system.out.println("son"); 17 } 18 public static void main(string[] args){ 19 //多态 20 baseclass nc = new subclass(); 21 system.out.println(nc.book);//输出6,对象的实例变量不具备多态性 22 nc.test();//输出son's method covers father's ,执行的是subclass中重写的方法 23 nc.base();//father loves you,subclass继承的方法 24 //nc.sub(); 会报错,因为baseclass 不具备sub方法 25 } 26 }
这个例子中 nc 的编译时类型是 baseclass , 运行类型是 subclass,在调用 nc 的方法时,总是显现除 subclass 的行为特征。但是注意,实例变量不具有多态性。
可以这么理解,子类对象建立时其实也创建了一个父类类型的对象,若编译时类型是父类,运行时类型是子类,该变量运行子类和父类拥有的共同的方法依然保持子类的特征(依然使用子类重写的方法),无法使用子类独有的方法,而且只能使用父类的实例变量。
引用变量在编译时,只能调用其编译时类型所具有的方法,但运行时则执行它运行时类型所具有的方法;在访问其包含的实例变量时,系统总是访问它编译时类型所定义的成员变量,而不是运行时类型。
1.2 强制转换
引用类型的转换只能发生在具有继承关系的两个类型之间,如果试图把一个父类的实例转换成子类类型时,则这个对象必须实际上是子类实例才行(运行时类型是子类)。子类转换成父类总是可以成功的。但父类转换成子类不一定。
类型转换之前,最好先通过 instanceof 运算符判断。
1 if (obj1 instanceof type2){ 2 type2 obj2 = (type2)obj1; 3 }
1.3 关于继承和多态的个人理解
1.3.1 继承
在学习继承和多态的过程中,发现了一个现象,当子类声明了一个和父类同名的变量时,子类的实例其实同时拥有了这两个实例变量,只是默认情况下父类的那个变量会被隐藏,接下来用代码测试展示。
1 class father { 2 int i = 0; 3 4 public int geti() { 5 return i; 6 } 7 8 public void seti(int i) { 9 this.i = i; 10 } 11 12 public void print() { 13 system.out.println("i:" + i); 14 } 15 } 16 17 public class son extends father { 18 int i = 1; 19 20 public void print() { 21 system.out.println(i); 22 } 23 24 public static void main(string[] args) { 25 son son = new son(); 26 son.seti(2); 27 son.print(); 28 system.out.println(son.i); 29 system.out.println(son.getsuper()); 30 son.superprint(); 31 } 32 33 public int getsuper() { 34 return super.i; 35 } 36 public void superprint() { 37 super.print(); 38 } 39 }
在这个程序里,父类声明 i 时赋值为 0,子类也声明了一个 i 赋初值为1,子类重写了父类的 print()方法,为表示区分,父类的print()方法加了字符串“i:”。
- main 方法中,新建了一个子类对象后,从子类对象调用继承来的 setter 方法改变 i 的值为 2,接着调用子类重写的 print()方法。打印的值依然为 “1”;
- 用常规的 println()方法打印 son.i 的值,也是“1”;
- 用子类的 getsuper() 方法获得父类的 i 的值,并将其打印,打印的值为 “2”;
- 用子类的 superprint()方法,调用父类的 print()方法,打印值为“i: 2”;
由我之前写的一批文章中可知,当子类重写了父类的方法后,父类的方法其实只是被覆盖了,可以用 super 限定调用,其实实例变量也是一样的。
另外值得提出的是,系统在执行方法时,查找某一变量的顺序:
- 查找该方法中是否有名为 a 的局部变量;
- 查找当前类中是否包含名为 a 的成员变量;
- 查找 a 的直接父类中是否有名为 a 的成员变量,依次上溯到 a 的所有父类,这种查找只会往继承树的上方找。
所以我们可以解释上述程序的现象了。当我们调用 setter 方法时,setter 在其当前类也就是父类中找到了成员变量 i ,于是改变其值为 2;调用 son 的 print()方法时,在其当前类也就是子类找到了 成员变量 i ,所以打印值为 1;用常规方法打印 son.i 时,父类的 i 已经被覆盖,所以打印值也是 1;用 getsuper()方法获得父类的 i 并将其打印发现,其值确实被修改成了 2;用 superprint()方法调用父类的 print()方法,该方法在当前类找到了变量 i,于是将其打印,结果为:“i: 2”。
在继承的学习中,我们知道,当我们创建一个子类实例时,系统会从继承树的顶端开始,依次往下初始化类,接着创建各个类的实例。根据上文的例子,我的理解是,与其说继承后子类“拥有”了父类的属性和方法,倒不如说是“借用”。当我们由子类实例调用父类的方法或访问父类的成员变量(子类中没有重写的方法\没有再次声明的成员变量),系统在子类下没有找到同名的方法或变量,于是由下往上在其父类中寻找并调用\访问,这就造成了子类“拥有”了父类的成员变量及方法的假象。一旦子类中有同名的成员,父类成员就被覆盖。
1.3.2 多态
利用上面继承所说的“覆盖”的特性,也可以解释一些多态的现象。
1 class father{ 2 int i = 1; 3 public void print(){ 4 system.out.println("father"); 5 } 6 public void printnum(){ 7 system.out.println(i); 8 } 9 } 10 public class son extends father{ 11 int i = 2; 12 13 public void print(){ 14 system.out.println("son"); 15 } 16 public void printnum(){ 17 system.out.println(i); 18 } 19 public static void main(string[] args) { 20 father val = new son(); 21 val.print(); 22 system.out.println(val.i); 23 val.printnum(); 24 } 25 }
程序的输出是:
第一条和第二条我们由上文“多态性”中不难理解,总结一句话就是:“方法有多态性,实例变量没有多态性”,寻找变量时系统总是由编译时类型出发,而寻找方法时系统总是由运行时类型出发。而第三条输出结果,我们就可以借由上文所说的“隐藏”特性以及“多态性”一同理解:当 val 调用 printnum()方法时,系统由其运行时类型出发,找到了子类的重写的 printnum()方法,该方法又由当前类(子类)出发寻找变量 i ,于是找到了被隐藏了的值为 2 的变量 i 。
2 leetcode
38.报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1
被读作 "one 1"
("一个一"
) , 即 11
。11
被读作 "two 1s"
("两个一"
), 即 21
。21
被读作 "one 2"
, "one 1"
("一个二"
, "一个一"
) , 即 1211
。
给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。
注意:整数顺序将表示为一个字符串。
示例 1:
输入: 1
输出: "1"
示例 2:
输入: 4
输出: "1211"
题目比较晦涩难懂。。。(外国人的脑洞)反正就是数组第一项是1,之后每一项都是把前一项像报数一样念出来,再将念出来的数变成数字序列。(好像我也没说清楚。。)
我的回答是:
1 class solution { 2 public string countandsay(int n) { 3 string[] array = new string[n]; 4 array[0] = "1"; 5 stringbuilder tmp = new stringbuilder(); 6 int count = 1; 7 for(int i = 0; i < n - 1; i++){ 8 for(int j = 0; j < array[i].length(); j++){ 9 if(j < array[i].length() - 1 && array[i].charat(j) == array[i].charat(j + 1)){ 10 count++; 11 continue; 12 } 13 tmp.append(count); 14 tmp.append(array[i].charat(j)); 15 count = 1; 16 } 17 array[i + 1] = tmp.tostring(); 18 tmp.delete(0, tmp.length()); 19 } 20 return array[n-1]; 21 } 22 }
另附评论找到的最快方法(手动滑稽),其实是方便大家理解题意:
1 class solution { 2 public string countandsay(int n) { 3 switch(n){ 4 case 1: 5 return "1"; 6 case 2: 7 return "11"; 8 case 3: 9 return "21"; 10 case 4: 11 return "1211"; 12 case 5: 13 return "111221"; 14 case 6: 15 return "312211"; 16 case 7: 17 return "13112221"; 18 case 8: 19 return "1113213211"; 20 case 9: 21 return "31131211131221"; 22 case 10: 23 return "13211311123113112211"; 24 case 11: 25 return "11131221133112132113212221"; 26 case 12: 27 return "3113112221232112111312211312113211"; 28 case 13: 29 return "1321132132111213122112311311222113111221131221"; 30 case 14: 31 return "11131221131211131231121113112221121321132132211331222113112211"; 32 case 15: 33 return "311311222113111231131112132112311321322112111312211312111322212311322113212221"; 34 case 16: 35 return "132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211"; 36 case 17: 37 return "11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221"; 38 case 18: 39 return "31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211"; 40 case 19: 41 return "1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221"; 42 case 20: 43 return "11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211"; 44 case 21: 45 return "311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221"; 46 case 22: 47 return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113213221133122112231131122211211131221131112311332211211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211"; 48 case 23: 49 return "111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121113222123112221221321132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221"; 50 case 24: 51 return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211"; 52 case 25: 53 return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132132211231232112311321322112311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312111312212231131122211311123113322112111312211312111322111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113213221132213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121132211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221"; 54 case 26: 55 return "1113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123211211131211121311121321123113111231131122112213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122113221122112133221121113122113121113222123211211131211121311121321123113213221121113122113121113222113221113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211"; 56 case 27: 57 return "31131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322123211211131211121332211231131122211311122122111312211213211312111322211231131122211311123113322112111331121113112221121113122113111231133221121113122113121113222123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221123113112221131112311332111213122112311311123112111331121113122112132113311213211321222122111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311123113322113223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331221122311311222112111312211311123113322112132113213221133122211332111213112221133211322112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121122132112311321322112111312211312111322211213111213122112132113121113222112132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212321121113121112133221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213212312311211131122211213211331121321122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311222113111221221113122112132113121113222112132113213221133122211332111213322112132113213221132231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221"; 58 case 28: 59 return "13211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221232112111312211312113211223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321322113311213212322211322132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212311322123123112111321322123122113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132132211331221122311311222112111312211311123113322112111312211312111322212311322123123112112322211211131221131211132221132213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211"; 60 case 29: 61 return "11131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221231122212213211321322112311311222113311213212322211211131221131211132221232112111312111213322112131112131221121321131211132221121321132132212321121113121112133221121321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122111213122112311311222113111221131221221321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311222112111331121113112221121113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212321121113121112133221132211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112132132112211131221131211132221121321132132212321121113121112133221123113112221131112311332111213211322111213111213211231131211132211121311222113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131211131221223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112211213322112312321123113213221123113112221133112132123222112311311222113111231132231121113112221121321133112132112211213322112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212311222122132113213221123113112221133112132123222112311311222113111231133211121321132211121311121321122112133221123113112221131112311332211322111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221"; 62 case 30: 63 return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211"; 64 } 65 } 66 }
上一篇: Java基础练习1(数据类型转换)
下一篇: php 字符串替换的方法