一.适配器
1.类适配器:让没有关系的类和接口产生联系,扩展功能,增加已有代码的复用性
2.对象适配器:io 使用对象进行连接 扩展功能
3.缺省适配器:使用一个类作为桥梁,连接接口与类,让适配器类来实现接口---空实现
1.类适配器
使用继承的管理来实现,让类和接口产生联系
使用一个是适配器类(第三方的类)通过这个类产生联系,提高接口的兼容性
class IPhone7{
public void listenMusic() {
System.out.println("使用充电口听");
}
}
interface Target{
public abstract void connection();
}
class Adapter extends IPhone7 implements Target{
@Override
public void connection() {
System.out.println("使用转接头");
super.listenMusic();
}
}
2.缺省适配器
interface JSF{
public abstract void paoBuJi();
public abstract void woTui();
public abstract void yaLing();
}
abstract class MyAdapter implements JSF{
@Override
public void paoBuJi() {
}
@Override
public void woTui() {
}
@Override
public void yaLing() {
}
}
class CDD extends MyAdapter{
@Override
public void paoBuJi() {
System.out.println("跑步半小时");
}
}
二.字符串
1.
相当于修改的是字符串的引用(地址)
指向了不同的地址,而不是字符串本身被修改
String s1="abc";
s1="aaa";
System.out.println(s1);
2.
s2的创建方式 相当于 在方法区的常量池中创建一个字符串
s3是在堆内存中开辟一块空间
String s2="zzz";
String s3=new String("zzz");
String s4="zzz";
System.out.println(s2==s4);
System.out.println(s2.equals(s4));
System.out.println(s2==s3);
结果: true
true
false
3.String string="wanglong";
//角标不要越界
char c = string.charAt(5);
System.out.println(c);
//获取索引
String s1="wanglong";
int index=s1.indexOf('g');//97数字也行
System.out.println(index);
//从传入的索引这一位开始查找
int index1=s1.indexOf('g', 4);//返回7
System.out.println(index1);
//传入字符串 查找对应角标
int index2=s1.indexOf("olong");
System.out.println(index2);
4.
String string="www.baidu.com";
//判断包含
boolean rel1 = string.contains("baidu");
System.out.println(rel1);
//判断前缀
boolean rel2 = string.startsWith("www");
System.out.println(rel2);
//判断后缀 .txt .png
boolean rel3 = string.endsWith("com");
System.out.println(rel3);
//判断两个字符串相等
boolean rel4 = string.equals("haha");
System.out.println(rel4);
//判断两个字符串忽略大小写相等 验证码
boolean rel5 = string.equalsIgnoreCase("wanglong");
System.out.println(rel5);
//字符串转小写
String s1 = string.toLowerCase();
System.out.println(s1);
//字符串转大写
String s2 = string.toUpperCase();
System.out.println(s2);
5.
//判断字符串是否空
String string="";
boolean rel=string.isEmpty();
System.out.println(rel);
//字符串拼接
String s1="wang";
String s2="long";
String s3=s1+s2;
System.out.println(s3);
String s4 = s1.concat(s2).concat("haha");
System.out.println(s4);
6.
// 把字符数组转化为字符串
char[] array = {'w','a','x','q','g'};
String string = new String(array);
System.out.println(string);
// 把字符串转化为字符数组
String string2 = "zxcvasq";
char[] charArray = string2.toCharArray();
for (char c : charArray) {
System.out.println(c);
}
7.
//去空格
String string=" asd sdf asasd ";
String s1 = string.trim();
System.out.println(s1);
//字符串比较
String s2="abcAB";
String s3="AB";
//相等返回 0 返回整数 前面大 负数 前面小
//字符串不一样 按ASCII码表 返回两个字符差值
//长度不一样 返回 位数 的差值
//一位一位进行比较 字符不一样就做差值
int num = s2.compareTo(s3);
System.out.println(num);
8.
//获取子字符串
String string="wanglong";
//从传入的索引这一位开始截取(包括3)
String s1 = string.substring(3);
System.out.println(s1);
String s2 = string.substring(0, 3);
System.out.println(s2);//wan 留头不留尾
9.
//字符串分割 返回值 数组 String[]
String string="wanglong,caodongdong,asdasd";
//如果是"." 就"\\." 使用转义符
String[] strings = string.split(",");
System.out.println(Arrays.toString(strings));
//增强for循环 用来遍历
for(String s:strings){
// String s 相当于容器中每一个元素
System.out.println(s);
}
10.
String string="wanglong";
String s1 = string.replace('w', 'l');
System.out.println(s1);
//替换字符串
String s2 = string.replaceAll("wang", "ba");
System.out.println(s2);
11.
//基本数据类型转化为字符串
String string = String.valueOf(false );
System.out.println(string);