目录
1:String类常用方法总结
String类代表的是字符串,是不可变的字符序列
public class TestString {
public static void main(String[] args) {
//Sting类代表字符串,是不可变字符序列
String str1=new String("abcdefg");
String str2="abcdefg";
String str3="abcdEFG";
//涉及到字符串比较的时候都用equals
System.out.println(str2.equals(str2));
System.out.println(str1.length());
//返回当前索引位置的字符
System.out.println(str1.charAt(0));
//取字符串最后一个字符
System.out.println(str1.charAt(str1.length()-1));
System.out.println(str3.equalsIgnoreCase(str2));//true
//从开头查到结尾,返回第一个子字符串的索引位置,如果未找到,则返回-1
//通常判断一个字符串是否包含另外一个字符串,若不包含则返回-1
System.out.println(str2.indexOf("def"));//3
System.out.println(str2.indexOf("h"));//-1
//字符串替换
System.out.println(str3.replace("ab","ABab"));
System.out.println(str2.replace('a','A'));
//判断以什么开头结尾
System.out.println("Hello world!!".startsWith("Hello"));//true
System.out.println("Hello world".endsWith("world"));//true
//提取子字符串
System.out.println("abcdefhijklmnopqrstuvwxyz"
.substring(0));
//abcdefghijklmnopqrstuvwxyz,返回[0,str.length()-1]处的字符串
System.out.println("abcdefhijklmnopqrstuvwxyz".substring(0,11));
//abcdefhijkl 返回[0,10]处的字符串
//大小写转换
System.out.println("abcdef".toUpperCase());
System.out.println("ABCDEF".toLowerCase());
//去掉首尾的空格多用于带有输入名字的系统
String str4=" a b ";//长度为7
String str5=str4.trim();
System.out.println(str5.length());//3
System.out.println(str4.replace(" ",""));
}
}
2:内部类
内部类的作用:
内部类提供了更好的封装。只能让外部类直接访问,不允许同一个包中的其他类直
接访问。
内部类可以直接访问外部类的私有属性,内部类被当成其外部类的成员。 但外部类
不能访问内部类的内部属性。
1.1非静态内部类
public class TestOuter {
public static void main(String[] args) {
//方法一
TestOuter.Inner inner=new TestOuter().new Inner();
inner.show1();
//方法二
TestOuter Test=new TestOuter();
TestOuter.Inner inner2=Test.new Inner();
}
private int age=10;
private String neme1="古倪啷虚宇";
public void show(){
System.out.println("要你好看!!!");
}
class Inner{
private int age=20;
private int heavy1=63;
public void show1(){
show();//内部类可以直接调用外部类的成员
System.out.println("Inner.shouw1");
System.out.println(age);
//当内部类成员名和外部类成员名同名的时候使用外部类.this.外部类成员
//即可访问外部类的成员
System.out.println(TestOuter.this.age);
}
}
}
1.2:静态内部类
在静态内部类不可以访问外部类的非静态成员,
1.3局部内部类,在外部类中方法里面定义的类
1.4:匿名内部类
这种在实际开发中很少用