It彭于晏带你学JAVA之适配器模式及API

1.适配器模式

1.类适配器 
 * (没有关系的类和产生联系 可以扩展功能 增加已有代码的复用性)
 * 2.对象适配器(io 使用对象进行连接 来扩展功能)
 * 3.缺省适配器
* iphone6 3.5mm的插口听歌
 * iphone7 直接使用充电口 ---需要使用转换头
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		Target iphone6 = new IPhone6();
		iphone6.connection();
		// 使用适配器的类 创建接口使用多态的方式创建
		Target iPhone7 = new Adapter();
		iPhone7.connection();

	}
}

class IPhone7 {
	// 听音乐方法
	public void listenMusic() {
		System.out.println("直接使用充电口听");
	}
}

// 目标接口
// 当前接口 没有对Iphone7 进行 兼容
interface Target {
	// 耳机连接抽象方法
	public abstract void connection();
}

class IPhone6 implements Target {
	// 实现抽象方法
	@Override
	public void connection() {
		System.out.println("使用3.5mm听");
	}

}

// 使用继承的关系来实现 让类和接口 产生联系
// 使用一个适配器类(第三方的) 通过这个类
// 类和接口 产生联系 提高接口的兼容性
class Adapter extends IPhone7 implements Target {
	// 实现抽象方法
	@Override
	public void connection() {
		System.out.println("使用转接头");
		super.listenMusic();
	}

}


2.缺省适配器

* 缺省适配器(Severlet)
* 健身房规则(接口)
* 跑步机
* 史密斯架(卧推)
* 哑铃
* 
* 王龙 实现 健身房
* 健身房规则(接口)
* 跑步机
* 史密斯架(卧推)
* 哑铃
* -----> 死了
* 活着(只能实现跑步机)
*/ 
public class Demo02 {
	public static void main(String[] args) {
		WL wl = new WL();
		wl.paoBuji();
	}

}
//健身接口
interface JSF{
	//跑步方法
	public abstract void paoBuJi();
	//卧推方法
	public abstract void woTui();
	//哑铃方法
	public abstract void yaLing();
}
//使用一个类作为桥梁 来连接 接口 与 类
//让适配器类 来实现接口 ---空实现
//适配器类 只是对接口方法的空实现 具体怎么实现 不管
//不希望 这个适配器类 直接被使用 因为它并没有对方法进行真正的实现,所以才使用 abstract 修饰 保证无法创建对象.
abstract class MyAdapter implements JSF{
	@Override
	public void paoBuJi() {
	
	}

	@Override
	public void woTui() {
		
	}

	@Override
	public void yaLing() {
	
	}
	
}

//这是就可以直接继承 适配器类 
class WL extends MyAdapter{
	//重写适配器的方法
	public void  paoBuji() {
		System.out.println("跑步半小时");
	}
}

class KDL extends MyAdapter{
	@Override
	public void paoBuJi() {
		System.out.println("跑步三小时");
	}
	@Override
	public void yaLing() {
		System.out.println("10公斤100举");
	}
}

3.系统提供的API之String

/* 
 *  获取
	根据索引获取字符串中字符
	char charAt(int index)
	根据字符获取在字符串中的位置
	整数和字符 可以相互转换 ASCII+码表
	int indexOf(int ch);
	
	2.判断 返回值都是 布尔类型
 *   是否包含这个字符串
 *   是否以这个前缀开头
 *   是否以这个后缀结尾
 */
public class Demo02 {
	public static void main(String[] args) {
			fun1();
			String string = "wanglong";
			//判断包含
			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("hahaha");
			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);
	
	}

	//获取
	private static void fun1() {
		String string	="wanglong";
		//注意:角标不要越界
		char c = string.charAt(1);
		System.out.println(c);
		
		//获取索引
		String s1 = "wanglong";
		int index = s1.indexOf('g');
		System.out.println(index);
		//从传入的索引 这一位开始查找
		//[0.0,1.0) 留头不留尾
		int index2 = s1.indexOf('g', 4);
		System.out.println(index2);
		//传入字符串 查找对应的角标
		int  index3 = s1.indexOf("long");
		System.out.println(index3);
	}
}
/*

 *  替换 切割 获取子串 转换大小写 去空格 比较

	判断两个字符串相等(忽略大小写)

	把字符数组 转化为 字符串

	把字符串转化为 字符数组

	判断字符串是否为空

 */

public class Demo03 {

		public static void main(String[] args) {

			//fun1();

			//fun2();

			//fun3();

			//fun4();

			//fun5();

			//fun6();

			//fun7();

		}



		private static void fun7() {

			//基本数据类型 转化 为字符串(类方法)

			String string = String.valueOf(1);

			System.out.println(string);

		}



		private static void fun6() {

			//判断字符串是否是空(常用)

			String string = "";

			boolean rel = string.isEmpty();

			System.out.println(rel);

			//字符串拼接

			String  s1 = "wang";

			String  s2 = "long";

			String s3 = s1 + s2;

			String s4 = s1.concat(s2).concat("hahaha");

			System.out.println(s4);

		}



		private static void fun5() {

			// 把字符数组 转化为字符串

			char[] array = {'w','a','n','g'};

			//使用构造方法 转化

			String string = new String(array);

			System.out.println(string);

			//字符串转化成字符数组(常用)

			String s1 = "wanglong";

			char[] charArray = s1.toCharArray();

			for (char c : charArray) {

				System.out.println(c);

				

			}

		}



		private static void fun4() {

			//去空格

			String string = "   abc  def ";

			String s1 = string.trim();

			System.out.println(s1);

			

			//字符串比较

			String s2 = "abcAB";

			String s3 ="Ab";

			//相等返回0

			//返回正数 前面大

			//返回负数 前面小

			//字符串不一样的时候 按ASCLL表 返回两个字符的差值

			//长度不一样的时候 返回的是位数的 差值

			//一位一位进行比较 字符不一样 就做差值

			int num = s2.compareTo(s3);

			System.out.println(num);

		}



		private static void fun3() {

			//获取子串

			String string = "wanglong";

			//从传入的索引这一位开始截取(包括3)

			String s1 = string.substring(3);

			System.out.println(s1);

			//[0,3) 留头不留尾 实际截取[0,2]

			String s2 = string.substring(4, string.length());

			System.out.println(s2);

		}



		private static void fun2() {

			//fun1();

			//字符串分割 返回值 数组 String[]

			String string = "wanglong.pengqian.liushangkun";

			//使用转译符

			String[] strings = string.split("\\.");

			System.out.println(Arrays.toString(strings));

			//增强for循环(专门用来遍历的)

			//for(容器中数据类型 名字:遍历容器){

				

			//}

			for (String string2 : strings) {

				System.out.println(string2);

			}

		}



		private static void fun1() {

			//替换

			String string = "wanglong";

			String s1 = string.replace('w', 'l');

			System.out.println(s1);

			//替换字符串

			String s2 = string.replace("long", "ba");

			System.out.println(s2);

		}

}


4.String 方法的应用

/*
 * 一.将字符串"   abc def" 反转
 * 1.转换成字符数组
 * 2.计算换位次数(循环的次数)
 * 3.array[i] 和 array[length - 1- i]
 * 4.字符数组转字符串
 * 
 * 
 * 二.将字符串 "   abc def " 去除空格自己(实现)
 * 
 * 三. 计算 
 * "www" 在 "wwwasdwwwsnfjwwwsnsdkwwwjsd" 
 *  当中出现次数
 *  
 * 四. 打印下面两个字符串 中相同的 最大子字符串
 * asakjfahellosdlmkasklhka
 * mcnvbsehellolssphkp
 */
public class Demo04 {
	public static void main(String[] args) {
		// fun1();
		// fun2();
		// fun3();
		// 没见过 你就不会
		fun4();
	}

	private static void fun4() {
		int num = 0;
		String s1 = "asakjfahellosdlmkasklhka";
		String s2 = "mcnvbsehellolssphkp";
		// 以短的字符串 为参照物 查找
		// 声明循环增量
		int i = 0;
		while (i < s2.length()) {
			// 为了找到开始的索引
			int start = 0;
			// 结束的索引
			// length
			// length - 1
			int end = s2.length() - i;
			// 截取最大子串 从大到小截取
			while (end < s2.length()) {
				// 截取
				String substring = s2.substring(start, end + 1);
				System.out.println(substring);
				// 跟s1字符串 进行比对
				// 判断包含
				if (s1.contains(substring)) {
					// 第一个进来 就是最大子串
					System.out.println(substring);
					// 结束该方法
					return;
				}
				// 让开始和结束的坐标 向前移动
				start++;
				end++;
			}
			i++;
		}
	}

	private static void fun3() {
		// 声明保存次数
		int count = 0;
		String s1 = "wwwasdwwwsnfjwwwsnsdkwwwjsd";
		// 把能声明的变量 全声明一遍
		String key = "www";
		// 声明索引
		int index = 0;
		// 判断 该字符串中 是否包含 要查找的字符串
		while (s1.contains(key)) {
			// 查找 www 在字符串中的 索引
			index = s1.indexOf(key);
			// 截取字符串
			s1 = s1.substring(index + key.length());
			// 计数器++
			count++;
		}
		System.out.println(count);
	}

	private static void fun2() {
		String s1 = "   abc def  ";
		// 只要找出开始和结束的索引 就完事啦
		// 定义开始和结束的索引
		int start = 0;
		int end = s1.length() - 1;
		// 计算开始坐标
		while (s1.charAt(start) == ' ' && start < end) {
			// 从前开始 一位一位向后寻找
			start++;
		}
		// 计算结束坐标
		while (s1.charAt(end) == ' ' && start < end) {
			end--;
		}
		// 截取字符串
		String substring = s1.substring(start, end + 1);
		System.out.println(substring);
	}

	private static void fun1() {
		String string = " abc def   ";
		char[] charArray = string.toCharArray();
		// 换位
		for (int i = 0; i < charArray.length / 2; i++) {
			char c = charArray[i];
			charArray[i] = charArray[charArray.length - 1 - i];
			charArray[charArray.length - 1 - i] = c;
		}
		// 字符数组转字符串
		String string1 = new String(charArray);
		System.out.println(string1);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42120532/article/details/80343917