内部类
内部类的分类
成员内部类
静态内部类
局部内部类
匿名内部类
什么是内部类
概念:在一个类的内部再定义一个类
1 2 3 4 5 class Outer { class Inner { } }
特点:
编译之后可生成独立的字节码文件
内部类可直接访问外部类的私有成员,而不破坏封装
可为外部类提供必要的内部功能组件
上述代码编译之后:
Outer$Inner.class
Outer.class
内部类可以访问外部类的私有成员:
1 2 3 4 5 6 7 8 9 10 11 package com.category;public class Body { private String name; class Header { public void show () { System.out.println(name); } } }
成员内部类
在类的内部定义,与实例变量、实例方法同级别的类。
外部类的一个实例部分,创建内部类对象时,必须依赖外部类对象。
1 2 Outer out = new Outer ();Inner inner = out.new Inner ();
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.category;import com.category.Outer.Inner;public class TestOuter { public static void main (String[] args) { Inner inner= new Outer ().new Inner (); inner.show(); } }
当外部类、内部类存在重名属性时,会优先访问内部类属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.category;import com.category.Outer.Inner;public class TestOuter { public static void main (String[] args) { Inner inner= new Outer ().new Inner (); inner.show(); } }
❌ private static String country = “China”;
✔ private static final String country = “China”;
静态内部类
不依赖外部类对象,可直接创建或通过类名访问,可声明静态成员。
只能直接访问外部类的静态成员(实例成员需实例化外部类对象)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package com.category.Static;public class TestOuter { public static void main (String[] args) { Outer.Inner inner = new Outer .Inner(); inner.show(); } }
局部内部类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.category.part;public class TestOuter { public static void main (String[] args) { Outer outer = new Outer (); outer.show(); } }
匿名内部类
没有类名的局部内部类(一切特征都与局部内部类相同)
必须继承一个父类或者实现一个接口
定义类、实现类、创建对象的语法合并,只能创建一个该类的对象
优点:减少代码量
缺点:可读性差
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.category.anonymous;public class TestUsb { public static void main (String[] args) { Usb usb = new Usb (){ @Override public void service () { System.out.println("连接电脑成功,风扇开始工作了..." ); } }; usb.service(); } }
Object类
getClass()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.category;public class TestStudent { public static void main (String[] args) { Student s1 = new Student ("aaa" ,20 ); Student s2 = new Student ("bbb" ,18 ); Class class1 = s1.getClass(); Class class2 = s2.getClass(); if (class1==class2){ System.out.println("s1和s2属于同一个类型" ); }else { System.out.println("s1和s2不属于同一个类型" ); } } }
hashCode()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.category;public class TestStudent { public static void main (String[] args) { Student s1 = new Student ("aaa" ,20 ); Student s2 = new Student ("bbb" ,18 ); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); Student s3 = s1; System.out.println(s3.hashCode()); } }
toString()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.category;public class TestStudent { public static void main (String[] args) { Student s1 = new Student ("aaa" ,20 ); Student s2 = new Student ("bbb" ,18 ); System.out.println(s1.toString()); System.out.println(s2.toString()); } }
equals()方法
equals()方法步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 package com.category;public class TestStudent { public static void main (String[] args) { Student s1 = new Student ("aaa" ,20 ); Student s2 = new Student ("bbb" ,18 ); System.out.println(s1.equals(s2)); Student s4 = new Student ("小明" ,17 ); Student s5 = new Student ("小明" ,17 ); System.out.println(s4.equals(s5)); } }
finalize()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 package com.category.Static;import com.category.Student;public class TestStudent2 { public static void main (String[] args) { new Student ("aaa" ,20 ); new Student ("bbb" ,20 ); new Student ("ccc" ,20 ); new Student ("ddd" ,20 ); new Student ("eee" ,20 ); System.gc(); System.out.println("回收垃圾" ); } }
包装类
什么是包装类
基本数据类型所对应的引用数据类型
Object可统一所有数据,包装类的默认值是null
包装类对应
基本数据类型
包装类型
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character
类型转换与装箱、拆箱
8种包装类提供不同类型间的转换方式:
Number父类中提供的6个共性方法。
valueOf()静态方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package com.category.pack;public class Demo01 { public static void main (String[] args) { int num1 = 18 ; Integer integer1 = new Integer (num1); Integer integer2 = Integer.valueOf(num1); System.out.println("装箱" ); System.out.println(integer1); System.out.println(integer2); Integer integer3 = new Integer (100 ); int num2 = integer3.intValue(); System.out.println("拆箱" ); System.out.println(num2); int age = 30 ; Integer integer4 = age; System.out.println("自动装箱" ); System.out.println(integer4); int age2 = integer4; System.out.println("自动拆箱" ); System.out.println(age2); } }
基本类型和字符串之间转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.category.pack;public class Demo01 { public static void main (String[] args) { int n1 = 100 ; String s1 = n1+"" ; String s2 = Integer.toString(n1); System.out.println(s1); System.out.println(s2); String str = "150" ; int n2 = Integer.parseInt(str); System.out.println(n2); String str2 = "true" ; boolean b1 = Boolean.parseBoolean(str2); System.out.println(b1); } }
注意:需保证类型兼容,否则抛出NumberFormatException异常
整数缓冲区
Java预先创建了256个常用的整数包装类型对象
在实际应用当中,对已创建的对象进行复用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.category.pack;public class Demo02 { public static void main (String[] args) { Integer integer1 = new Integer (100 ); Integer integer2 = new Integer (100 ); System.out.println(integer1==integer2); Integer integer3 = 100 ; Integer integer4 = 100 ; System.out.println(integer3==integer4); Integer integer5 = 200 ; Integer integer6 = 200 ; System.out.println(integer5==integer6); } }
其中,自动装箱过程调用的是:
1 2 3 4 5 6 7 Integer integer3 = Integer.valueOf(100 );Integer integer4 = Integer.valueOf(100 );System.out.println(integer3==integer4); Integer integer5 = Integer.valueOf(200 );Integer integer6 = Integer.valueOf(200 );System.out.println(integer5==integer6);
valueOf在 -128~127之间
String类
字符串是常量,创建之后不可改变
字符串字面值存储在字符串池中,可以共享
1 2 3 4 5 6 7 8 9 package com.category.str;public class Demo01 { public static void main (String[] args) { String name = "hello" ; name = "张三" ; String name2 = "张三" ; } }
String s = “Hello”; 产生一个对象,字符串池中存储。
String s = new String(“Hello”); 产生两个对象,堆、池各存储一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.category.str;public class Demo01 { public static void main (String[] args) { String name = "hello" ; name = "张三" ; String name2 = "张三" ; String str = new String ("java" ); String str2 = new String ("java" ); System.out.println(str==str2); System.out.println(str.equals(str2)); } }
常用方法
public int length() :返回字符串的长度
public char charAt(int index) :根据下标获取字符
public boolean contains(String str) :判断当前字符串中是否包含str
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.category.str;public class Demo02 { public static void main (String[] args) { String content = "java是世界上最好的编程语言" ; System.out.println(content.length()); System.out.println(content.charAt(0 )); System.out.println(content.charAt(content.length()-1 )); System.out.println(content.contains("java" )); System.out.println(content.contains("php" )); } }
public char[ ] toCharArray():将字符串转换成数组
public int indexOf(String str) : 查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.category.str;import java.util.Arrays;public class Demo02 { public static void main (String[] args) { String content = "java是世界上最好的java编程语言,java真香" ; System.out.println(Arrays.toString(content.toCharArray())); System.out.println(content.indexOf("java" )); System.out.println(content.indexOf("java" ,4 )); System.out.println(content.lastIndexOf("java" )); } }
public String trim():去掉字符串前后的空格
public String toUpperCase():将小写转成大写
public boolean endWith(String str):判断字符串是否以str结尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.category.str;import java.util.Arrays;import java.util.Locale;public class Demo02 { public static void main (String[] args) { String content2 = " hello World " ; System.out.println(content2.trim()); System.out.println(content2.toUpperCase()); System.out.println(content2.toLowerCase()); String filename = "hello.java" ; System.out.println(filename.endsWith(".java" )); System.out.println(filename.startsWith("hello" )); } }
public String replace (char oldChar,char newChar);将旧字符替换成新字符
public String[ ] split(String str); 根据str做拆分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.category.str;import java.util.Arrays;import java.util.Locale;public class Demo02 { String content = "java是世界上最好的java编程语言,java真香" ; System.out.println(content.replace("java" ,"php" )); String say = "java is the best programing language,java xxxing" ; String[] arr= say.split("[ ,]+" ); System.out.println(arr.length); for (String string : arr){ System.out.println(string); } String s1 = "hello" ; String s2 = "HELLO" ; System.out.println(s1.equals(s2)); System.out.println(s1.equalsIgnoreCase(s2)); String s3 = "abc" ; String s4 = "xyz" ; System.out.println(s3.compareTo(s4)); String s5 = "abc" ; String s6 = "abcxyz" ; System.out.println(s5.compareTo(s6)); } }
案例演示
已知String str = “this a text”;
将str中的单词单独获取出来
将str中的text替换为practice
在text前面插入一个easy
将每个单词的首字母改为大写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.category.str;public class Demo03 { public static void main (String[] args) { String str = "this a text" ; String[] arr= str.split("[ ,]+" ); for (String string : arr){ System.out.println(string); } System.out.println(str.replace("text" ,"practice" )); System.out.println(str.replace("text" ,"easy text" )); for (int i = 0 ;i<arr.length;i++){ char first = arr[i].charAt(0 ); char upperFirst = Character.toUpperCase(first); String news = upperFirst+arr[i].substring(1 ); System.out.println(news); } } }
可变字符串
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全
StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全
和String区别:
效率比String高
比String节省内存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.category.str;public class Demo04 { public static void main (String[] args) { StringBuffer sb = new StringBuffer (); sb.append("java世界第一" ); System.out.println(sb.toString()); sb.append("java真香" ); System.out.println(sb.toString()); sb.insert(0 ,"我在最前面" ); System.out.println(sb.toString()); sb.replace(0 ,5 ,"hello" ); System.out.println(sb.toString()); sb.delete(0 ,5 ); System.out.println(sb.toString()); sb.delete(0 ,sb.length()); System.out.println(sb.toString()); } }
效率比String高案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.category.str;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;public class Demo05 { public static void main (String[] args) { long start = System.currentTimeMillis(); StringBuilder sb = new StringBuilder (); for (int i = 0 ;i<99999 ;i++){ sb.append(i); } System.out.println(sb.toString()); long end = System.currentTimeMillis(); System.out.println("用时:" +(end-start)); } }
BigDecimal类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.category.str;import java.math.BigDecimal;public class Demo06 { public static void main (String[] args) { double d1 = 1.0 ; double d2 = 0.9 ; System.out.println(d1-d2); double result = (1.4 -0.5 )/0.9 ; System.out.println(result); BigDecimal bd1 = new BigDecimal ("1.0" ); BigDecimal bd2 = new BigDecimal ("0.9" ); BigDecimal r1 = bd1.subtract(bd2); System.out.println(r1); BigDecimal r2 = bd1.add(bd2); System.out.println(r2); BigDecimal r3 = bd1.multiply(bd2); System.out.println(r3); BigDecimal r4 = new BigDecimal ("1.4" ).subtract(new BigDecimal ("0.5" )).divide(new BigDecimal ("0.9" )); System.out.println(r4); BigDecimal r5 = new BigDecimal ("20" ).divide(new BigDecimal ("3" ),2 ,BigDecimal.ROUND_HALF_UP); System.out.println(r5); } }
时间类型
Date类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.category.qita;import java.util.Date;public class Demo01 { public static void main (String[] args) { Date date1 = new Date (); System.out.println(date1.toString()); System.out.println(date1.toLocaleString()); Date date2 = new Date (date1.getTime()-60 *60 *24 *1000 ); System.out.println(date2.toLocaleString()); boolean b1 = date1.after(date2); System.out.println(b1); boolean b2 = date1.before(date2); System.out.println(b2); int d = date1.compareTo(date2); System.out.println(d); boolean b3 = date1.equals(date2); System.out.println(b3); } }
Calendar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.category.qita;import java.util.Calendar;public class Demo02 { public static void main (String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toLocaleString()); System.out.println(calendar.getTimeInMillis()); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println(year+"年" +(month+1 )+"月" +day+"日" +hour+":" +minute+":" +second); Calendar calendar2 = calendar.getInstance(); calendar2.set(Calendar.DAY_OF_MONTH,7 ); System.out.println(calendar2.getTime().toLocaleString()); calendar2.add(Calendar.HOUR,1 ); System.out.println(calendar2.getTime().toLocaleString()); calendar2.add(Calendar.MONTH,1 ); int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH); int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH); System.out.println(max); System.out.println(min); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.category.qita;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class Demo03 { public static void main (String[] args) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" ); Date date = new Date (); String str = sdf.format(date); System.out.println(str); Date date2 = sdf.parse("2022/04/08" ); System.out.println(date2); } }
System 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.category.qita;public class Demo04 { public static void main (String[] args) { int [] arr = {20 ,18 ,15 ,8 ,35 ,26 ,45 ,90 }; int [] dest = new int [8 ]; System.arraycopy(arr,0 ,dest,0 ,arr.length); for (int i = 0 ;i<dest.length;i++){ System.out.println(dest[i]); } System.out.println(System.currentTimeMillis()); long start = System.currentTimeMillis(); for (int i = 0 ;i<99999 ;i++){ for (int j = 0 ;j<99999 ;j++){ int result = i+j; } } long end = System.currentTimeMillis(); System.out.println("用时" +(end-start)); new Student ("aaa" ,19 ); new Student ("bbb" ,19 ); new Student ("ccc" ,19 ); System.gc(); System.exit(0 ); System.out.println("程序结束了..." ); } }