知識點
接口實現回調,即接口的代理設計模式
- 抽象類 普通類 接口
- 1.是否需要添加成員變量
需要:抽象類 普通類
不需要:接口 - 2.添加的方法是否必須要實現
必須:抽象類 接口
不需要: 抽象類 普通類 - 3.需要提供模板還是通訊方式
模板: 抽象類
通訊(數據傳遞):接口
String
1.不可變的字符串 一旦創建 內部不能改變
- == 比較兩個對象是否相同:地址
- equals 比較內容是否相同
3.字符串的創建 - StringBuffer 可變字符串
- StringBuilder 可變字符串
技術的實際使用
模擬聊天字體等相關設定
聊天界面
public class Chat implements Set.FontSettingInterface {
String text;
String color= "紅色";;
int size = 15;
public Chat(String text){
this.text = text ;
System.out.println(text);
System.out.println("改變前的顏色:"+color+" 改變前的大小:"+size);
}
public void gotoSet() {
Set set = new Set(this);
set.startSeting();
}
public void change (String color,int size){
this.color = color;
this.size = size;
System.out.println("改變后的顏色:"+color+" 改變后的大小:"+size);
}
}
設置
public class Set {
FontSettingInterface c1;
public Set(FontSettingInterface c1) {
this.c1 = c1;
}
public interface FontSettingInterface{
//自己規定的方法
void change(String color,int size);
}
public void startSeting(){
System.out.println("開始設置");
System.out.println(".......");
System.out.println("設置成功");
c1.change("黑色",20);
}
}
隨筆
學習了一天,接口和抽象類依然掌握的不是很好。不看東哥的演示demo,勉強寫出了一個類似的聊天字體設置的demo,但是對于接口的使用還是不怎么順手,依然有一種云里霧里,似懂非懂的感覺。