一、學習目的
代理模式是Java常見的設計模式之一。所謂代理模式是指客戶端并不直接調用實際的對象,而是通過調用代理,來間接的調用實際的對象。
二、學習內容
(1)字符串
String類
1,String創(chuàng)建字符串的方法
//第一種
String str1 = "abc";
String是系統(tǒng)提供的類,因此可以使用構造方法創(chuàng)建一個字符串,但沒有意義,
//第二種
String str3 = new String(); //無意義
2, 字符串的比較:
“==”比較兩個對象是否相同,即比較地址是否相同
-
equals方法比較兩個字符串的內容是否相同
String str1 = "abc"; String str2 = "abc"; System.out.println(str1 == str2); System.out.println(str1.equals(str2));

輸出結果均為true,原因是str1與str2都是指向“abc”,所以內存地址相同
3,String創(chuàng)建的字符串是不可變的 一旦創(chuàng)建內容不可改變
4,使用字節(jié)數(shù)組byte創(chuàng)建一個字符串
//使用字節(jié)數(shù)組 創(chuàng)建一個字符串
byte[] name = {'x','w','j'};
String str4 = new String(name);
System.out.println(str4);
byte[] name2 = {97,98,99};
String str5 = new String(name2); //ASCII碼也可轉化成字符串
5,提取字節(jié)輸出的一部分創(chuàng)建字符串
//使用字節(jié)數(shù)組的一部分 創(chuàng)建一個新的字符串
String str6 = new String(name,0,2); //
System.out.println(str6);
6,字符數(shù)組轉化成字符串,char占兩個字節(jié)
char[] hello = {'你','好','啊'};
String h = new String(hello);
System.out.println(h);
7,獲取字符串的第一個字符用 charAt方法
char c = str4.charAt(0); //零就是第一個字符
8,兩個字符串的比較 :可以知道大小關系 返回值 為0 則相同 ,大于零就是表示大于 ,小于零就是表示小于
compareTo按字典順序比較 ,區(qū)分大小寫
compareToIgnoreCase忽略大小寫比較
int result = str4.compareTo(str5);
System.out.println(result);
9,將兩個字符串連接起來,用concat方法
String nStr = str5.concat(str7);
System.out.println(nStr);
10,判斷一個字符串是否包含另外一個字符串,用contains方法
// contains
boolean b = "hello".contains("ll");
System.out.println(b);
11,判斷是否以某個字符串開頭或結尾,用endsWith和startsWith方法
String url = "http//www.baidu.com";
if(url.endsWith(".com")){
System.out.println("網(wǎng)址",7);
}
if(url.startsWith("http")){
System.out.println("http協(xié)議");
}
if(url.startsWith("www")){
System.out.println("萬維網(wǎng)");
}
12, 判斷一個字符串在另一個字符串里面的位置用 IndexOf方法,若存在則返回該字符串第一個字符出現(xiàn)的位置, 若不存在則返回-1
String il = "hello java";
int index = il.indexOf("java");
System.out.println(index);
13,獲取子字符串 subString
//從index到結尾
String sStr = il.substring(6);
System.out.println(sStr);
//從1到5的字符組成新的字符串
String sStr1 = il.substring(1,5);
System.out.println(sStr1);
14, 將字符串轉化為字符數(shù)組用toCharArray方法;將所有字符轉化為小寫 toLowerCase / toUpperCase方法; 將字符串前面和后面的空格字符刪除用 trim方法
15,可變字符串
StringBuffer 編程安全;StringBuilder 線程不安全 效率更高
//創(chuàng)建的同時先準備好6個字符的空間
StringBuilder sb = new StringBuilder(6);
// append 在末尾追加
sb.append("I");
sb.append(" Love");
sb.append(" Android");
System.out.println(sb);
// insert 插入數(shù)據(jù)
sb.insert(2, "also ");
System.out.println(sb);
// replace 替換
// start end string
int start = sb.indexOf("Android");
int end = start + "Android".length();
sb.replace(start,end,"you!!!");
System.out.println(sb);
// reverse 反轉
sb.reverse();
System.out.println(sb);
}
}
三、代理設計模式
項目:設計一個程序改變字體大小和顏色的功能
方法一:
1,先創(chuàng)建一個閱讀界面
public class Read{
private String text;
private String color;
private int size;
public Read(String text,String color,int size){
this.text = text;
this.color = color;
this.size = size;
}
//模擬進入設置頁面
public void goToSetting(){
//1,創(chuàng)建設置頁面的對象
Setting setting = new Setting(this);
//2,推送到設置頁面
setting.starSetting();
}
//提供給外部一個方法,可以通過這個方法為我賦值
public void change(String color,int size){
System.out.println("改變前的顏色"+this.color+"改變前的字體大小"+this.size);
this.color = color;
this.size = size;
System.out.println("改變后的顏色"+this.color+"改變后的字體大小"+this.size);
}
}
在設置一個設置類
public class Setting{
Read deleance; //用來記錄為誰設置顏色和大小 或記錄設置完成后將數(shù)據(jù)返回給誰
//創(chuàng)建設置頁面對象的時候,就需要告訴設置頁面是誰創(chuàng)建的,也就是這個界面因誰而創(chuàng)建
public Setting(Read deleance){
this.deleance = deleance;
}
//開始設置 需要返回給
public void startSetting(){
System.out.println("開始設置");
System.out.println("即將返回數(shù)據(jù)");
//如果可以直接訪問屬性 直接通過屬性給值
//比較少用 對象沒辦法第一時間知道自己需要的值已經(jīng)得到
deleance.change("red",18);
}
}
但此時加入多一個聊天界面想要使用這個setting類更改字體和顏色,就必須在Setting類面寫兩個構造方法,雖然可以通過多態(tài)的方式處理,但在后面調用change方法是還要用instanceof找到具體的類,另外,在兩個界面都調用的時候還會出現(xiàn)空指針異常,顯然這不是一種好的方法。此時可以使用接口實現(xiàn)回調,即代理設計模式
方法二:
1,閱讀界面
package swu.xwj.day7;
import swu.xwj.day7.Setting.FontSeetingInterface;
/**
* 閱讀界面
*/
public class Read implements FontSeetingInterface {
private String text;
private String color;
private int size;
public Read(String text,String color,int size){
this.text = text;
this.color = color;
this.size = size;
}
public void goTOsetting(){
//1,創(chuàng)建一個設置頁面
Setting setting = new Setting(this);
//2,推送到設置頁面
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("設置前的顏色為"+this.color+"設置前的字體大小為"+this.size);
this.color = color;
this.size = size;
System.out.println("設置前的顏色為"+this.color+"設置前的字體大小為"+this.size);
}
}
2,聊天界面
package swu.xwj.day7;
/**
* 聊天界面
*/
public class Chat implements Setting.FontSeetingInterface {
private String color;
private int size;
public Chat(String color,int size){
this.color = color;
this.size = size;
}
public void goTOsetting(){
//1,創(chuàng)建一個設置頁面
Setting setting = new Setting(this);
//2,推送到設置頁面
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("設置前的顏色為"+this.color+"設置前的字體大小為"+this.size);
this.color = color;
this.size = size;
System.out.println("設置前的顏色為"+this.color+"設置前的字體大小為"+this.size);
}
}
3,設置界面
package swu.xwj.day7;
/**
* 設置界面 設置字體顏色和大小 此種方法復用性低
*/
public class Setting {
FontSeetingInterface object;
public Setting(FontSeetingInterface object) {
this.object = object;
}
//使用一個接口,強制所有的使用者實現(xiàn)這個方法
public interface FontSeetingInterface{
//自己規(guī)定的方法
void change(String color,int size);
}
public void startSetting(){
object.change("yellow",16);
}
}
4,主界面
package swu.xwj.day7;
/**
* 接口實現(xiàn)回調 代理設計模式
*/
public class MyClass {
public static void main(String[] args){
//創(chuàng)建閱讀界面
Read read = new Read("hello world","black",16);
//設置字體顏色
read.goTOsetting();
//創(chuàng)建聊天界面
Chat chat = new Chat("red",24);
//設置字體顏色
chat.goTOsetting();
}
}

四、感悟
今天學習的東西比較零碎,但都不算難理解,重點就在于代理設計模式,也就是用接口實現(xiàn)回調,解決好類與類之間的數(shù)據(jù)處理,接口的概念雖然簡單,但是實際操作比較困難,多聯(lián)系才能真正掌握它。