Java 基础学习之字符串的简单操作和适配器设计模式

一 适配器设计模式

1.适配器是什么?

    适配器模式将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)。


2.适配器分类

1.类适配器:

让没有关系的类或接口产生联系,可以扩展功能 .增加代码的复用性.

2.对象适配器

使用对象来连接 来扩展功能

3.缺省适配器

看一个充电器的例子:(类适配器)

public class Demo01 {
	public static void main(String[] args) {
		Target iphone6 = new IPhone6();
		iphone6.connection();
		// 使用适配器的类
		Target iphone7 = new ListenAdapter();
		iphone7.connection();
	}

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

	}
}
// 目标接口

interface Target{
	// 耳机连接的方法
	public abstract void connection();
}

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

class ListenAdapter extends IPhone7 implements Target{
// 实现抽象方法
	@Override
	public void connection() {
	System.out.println("使用转接头");
	//调用父类的专有方法
	super.listenMusic();
		
	}
	
}
使用3.5mm接口听
使用转接头
直接使用充电口听

2.缺省适配器:

健身房(接口)
 跑步机
 史密斯架(卧推)
 哑铃
public class Demo02 {

}
interface GYM {
	// 跑步
	public abstract void run();
	//卧推
	public abstract void woTui();
	//哑铃
	public abstract void yaLing();
}
// 使用一个类作为桥梁来连接 接口 与类
// 让适配器类来实现接口 -----空实现
//适配器类 只是对接口方法的空实现 ,不管具体怎么实现
// 不希望适配器类直接被实现
abstract class SportAdapter implements GYM{

	@Override
	public void run() {

	}

	@Override
	public void woTui() {
	
	}

	@Override
	public void yaLing() {
		
	}
	
}
// 这时可以继承 适配器类
class DH extends SportAdapter{
	// 重写适配器类的方法 
	@Override
	public void run() {
		System.out.println("跑步半小时");
	}
	@Override
	public void woTui() {
		System.out.println("卧推3小时");
	}
}

二 字符串的常用操作

主要是学习系统类的写法:

学习系统类的目的: 学习系统的方法 如何使用

 方法:

 关键字(有无 static 修饰) 方法的调用方式.

 返回值类型(给我返回一个什么结果)

 方法名

 参数列表(方法需要什么)


字符串的一些主要注意事项"
public class Demo01 {
	public static void main(String[] args) {
		//修改的是字符串的引用,实际上字符串并没有改变,改变的是地址.
		//看方法时 拼接 截取 等字符串的操作方法时,都是返回一个新的字符串 
	 String s1 = "abc";
		s1 = "123";
		System.out.println(s1);//123
		//s2 的创建方式 相当于在方法区的常量池中创建一个字符串  (字符数组)
		//s3 是在堆内存中开辟一块空间 
		 String s2="abc";
		 String s3 = new String("abc");
		 String s4 = "abc";
		 // == 对象的话比的是地址
		 System.out.println(s2==s4);//true
		 // equals 把两个字符串 变成字符数组 比较
		 System.out.println(s2.equals(s4));//true
		 System.out.println(s2==s3);//false
		 //s2和s3  有什么区别
		 //s2是一个对象
		 // s3 有两个对象 分别是 "abc" 和new出来的对象
	}

}

1.字符串的获取

根据索引获取字符串中的字符  charAt(int index)

根据字符获取其在字符串中的位置 indexOf (int ch)

String str = "dahai";
		char c = str.charAt(3);
		// 注意索引值不要越界
		System.out.println(c); //a
		// 获取索引
	String s1 = "woshidahdai";
	int index = s1.indexOf('o');
	System.out.println(index);//1
	// 从传入的索引 这一位开始查找
	//[0.0,1.0) 留头不留尾
	int index1 = s1.indexOf('d', 0);
	System.out.println(index1);//5
	int index2= s1.indexOf("ah");
	System.out.println(index2);//6

2.字符串中的判断 返回的是Boolean值

1.判断是否包含这个字符串  contains(charSquenece s)

2.判断前缀, startsWith()

3.判断后缀 endsWith()

//字符串的判断
		// 判断包含
		String string = "woshidahai";
		boolean rell=string.contains("dadiaohai");
		System.out.println(rell);
		//判断前缀,以什么开头
		boolean rel2 = string.startsWith("w1");
		System.out.println(rel2);//false
		
		//判断后缀,以什么结尾
		boolean rel3 = string.endsWith("i");
		System.out.println(rel3);//true
		// 判断两个字符串相等
		boolean rel4 =string.equals("dahai");
		System.out.println(rel4);
		// 判断两个字符串忽略大小写相等
		boolean rel5 = string.equalsIgnoreCase("WoshidahaI");
		System.out.println(rel5);
		
		//字符串转小写
		String rel6 = string.toLowerCase();
		System.out.println(rel6);
		//字符串转大写
		String rel7 = string.toUpperCase();
		System.out.println(rel7);
	}

3.字符串的替换 replace(oldvalue ,newvalue)

public static void fun() {
		// 替换
		String str = "河流,我是你的水源";
		String s1= str.replace("你", "谁");
		System.out.println(s1);
		//替换字符串
		String s2 = str.replace("河流", "大海");
		System.out.println(s2);
	}

4.字符串的分割  split()

public static void fun2() {
		// 字符串分割  返回字符串类型的数组
		String string = "wanglong.pengqian.liushnagkun";
		String [] arr = string.split("\\."); // 转义字符
		System.out.println(Arrays.toString(arr));//[wanglong, pengqian, liushnagkun]
		//增强for循环 专为打印遍历
	/*	for (容器中数据类型 名字:遍历容器) {
			表示数组中的每一个元素
		}*/
		for (String s:arr) {
			System.out.println(s);
		}
	}

5.获取子串  substring()

public static void fun3() {
		//获取子字符串  wanglong 
		String str1 = "woshidahai";
		String str2 =str1.substring(3);
		String str3 =str1.substring(3, 5);//包括start值 不包括end值[3,5) 留头不留尾
		System.out.println(str2);
		System.out.println(str3);
	}

6.去空格 trim()  字符串比较

public static void fun4() {
		//去空格
		String str = "    abc   def  ";
		String s1 = str.trim();
		System.out.println(s1);
		//字符串比较
		String str01 = "abcAB";
		String str02 = "Ab";
		//比较是一位一位的比较 字符不一样,就做差值返回
		//相等返回零
		//字符不一样时,按acill表 ,返回两个字符只差
		// 长度不一样时,返回的是位数的差值
		int compareTo = str02.compareTo(str01);
		System.out.println(compareTo);
	}

7.字符串 与字符数组的相互转化

public static void fun5() {
		//把字符数组 转化为 字符串
		char [] array = {'d','a','h','a','i'};
		//直接使用构造方法
		String string = new String(array);
		System.out.println(string);//dahai
		//把字符串转化为 字符数组
		String ss = "dahai";
		char[] cs = ss.toCharArray();
		for (char c :cs) {
			System.out.println(c);
		}
	}

8.判断字符串是否为空 拼接字符串

public static void fun6() {
		//判断字符串是否为空
		String string = "";
		boolean rel = string.isEmpty();
		System.out.println(rel);
		//字符串拼接
		String s1 = "da";
		String s2 = "hai";
		String s3 = s1+s2;
		System.out.println(s3);
		
		String s4 = s1.concat(s2).concat(s2);
		System.out.println(s4);
	}

9.基本数据类型转化为字符串

		String v1 = String.valueOf(10);
		int a = 10;
		System.out.println(v1);
		System.out.println(a);


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值