每天学一点,久了就能学会很多
量变 –> 质变
static 关键字
一旦用了 static 关键字,那么这样的内容就不再属于对象自己,而是属于类的
凡是本类的对象,都共享同一份
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Student{ private int id; private static int idCounter = 0; public Student() { this.id = ++idCounter; }
public Student(String name, int age) { this.name = name; this.age = age; this.id = ++idCounter; } }
|
如上,可以定义一个用 static 关键字修饰的成员变量 idCounter ,再定义一个普通的用 private 修饰的成员变量 id ,就可以实现每创建一个对象,都可以自动获取 id 号,而且是可以自增的
这就是利用了 static 这个关键字
idCounter 这个变量属于这个类,在内存中只存储一次,所有对象的这个变量的值都是这个
static 关键字不但可以修饰成员变量,还可以修饰成员方法
一旦使用static修饰成员方法,那么这个方法就成为了静态方法,静态方法不属于对象,是属于类的
概念和修饰成员变量差不多
static 关键字修饰的成员方法可以不创建对象就使用里面的方法,如果没有static关键字,那么必须先创建对象,再通过对象使用它
1 2 3 4 5 6 7 8 9 10 11
| public class MyClass { public void method(){ System.out.println("这是成员方法"); } public static void methodStatic(){ System.out.println("这是静态方法"); } public static void myMethod(){ System.out.println("自己的方法"); } }
|
- 无论是成员变量还是成员方法,如果使用了static,都推荐使用类名称调用
- 静态方法可以访问静态变量,但不能直接访问非静态变量,反过来可以
- 静态方法不能使用this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class methodStatic { public static void main(String[] args) { MyClass obj = new MyClass(); obj.method();
obj.methodStatic(); MyClass.methodStatic();
myMethod(); methodStatic.myMethod(); } public static void myMethod(){ System.out.println("my method"); } }
|
字符串方法
字符串索引
indexOf 获取字符串第一次出现的位置,如果没有则返回-1
1 2 3
| String original = "HelloWorld"; int index = original.indexOf("llo"); System.out.println("索引结果是" + index);
|
charAt 获取特定位置的字符
1 2
| char ch = "Hello".charAt(1); System.out.println("一号位的字符是" + ch);
|
split 方法
和 Python 的 split 方法差不多,通过特定的字符将字符串分割
1 2
| String str = "Hello,world,!"; String[] arr1 = str.split(",");
|
- 不能使用 ‘.’ 来切割,若要使用,需要使用 ‘\.’,因为这个是正则表达式的一个符号
concat 方法
用以连接字符串
1 2 3
| String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat(str2);
|
替换方法
replace 方法用来替换字符串
1 2 3 4
| String lang1 = "fuck you!"; String lang2 = lang1.replace("uc", "**"); System.out.println(lang1); System.out.println(lang2);
|
toCharArray 方法,字符串转换为字符数组
1 2 3
| char[] chars = "Hello".toCharArray(); System.out.println(chars[0]); System.out.println(chars.length);
|
getBytes 方法,字符串转换为字节数组
1 2 3 4
| byte[] bytes = "abc".getBytes(); for (int i = 0; i < bytes.length; i++) { System.out.println(bytes[i]); }
|
字符串截取方法
- substring(int index):截取从参数位置一直到字符串末尾,返回新字符串
- substring(int begin, int end):截取从begin开始到end结束中间的字符串
1 2 3 4 5 6 7
| String str1 = "HelloWorld"; String str2 = str1.substring(5); System.out.println(str1); System.out.println(str2);
String str3 = str1.substring(4, 7); System.out.println(str3);
|
对比字符串是否相等
- equals(Object obj) 只有参数是一个字符串,且内容相同的才会返回true,否则返回false
- equalsIgnoreCase(Object obj) 忽略大小写
语法
1 2
| str1.equals(str2); strA.equalsIgnoreCase(strB);
|
ArrayList
对于ArrayList来说,有一个尖括号代表泛型
泛型:装在集合当中的所有元素,全都是统一的类型
泛型只能是引用类型,不能是基本类型
对于ArrayList集合,直接打印的不是地址值,而是内容
用法
1 2 3
|
ArrayList<String> list = new ArrayList<>();
|
方法
1 2 3 4 5 6 7 8 9 10 11
| boolean success = list.add("piapiapia~"); System.out.println("返回结果是" + success);
String word = list.get(2); System.out.println("索引为2的是" + word);
String move = list.remove(2); System.out.println("被删掉的是" + move);
int size = list.size(); System.out.println("集合长度是" + size);
|
若需要用基本类型,则需要使用基本类型的包装类
- jdk1.5开始支持自动装箱、拆箱
- 自动装箱,基本类型 –> 包装类型
- 自动拆箱,包装类型 –> 基本类型
1 2
| ArrayList<Integer> listA = new ArrayList<>(); ArrayList<Character> listA = new ArrayList<>();
|
其他的类型就是开头字母大写,除了以上两个
勤能补拙,学好了Java就可以开始学习Android开发了,然后就能申请项目赚钱拿奖了,芜湖