文章目录
1、内部类
1.1 内部类的定义
定义在内部的类
public class Body { // 外部类
public class Heart { // 成员内部类
// 内部类的方法
public void beat() {
System.out.println("心脏跳动!");
System.out.println("我叫:" + name);
}
}
// 外部类的成员变量
private String name;
// 外部类的方法
public void methodBody() {
System.out.println("外部类的方法");
new Heart().beat();//在外部类的方法中使用内部类的方法
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Demo01InnerClass {
public static void main(String[] args) {
Body body = new Body(); // 外部类的对象
// 通过外部类的对象,调用外部类的方法,里面间接在使用内部类Heart
body.methodBody();
System.out.println("=====================");
// 直接创建对象进行调用
Body.Heart heart = new Body().new Heart();
heart.beat();
}
}
内部类,外部类中变量重名,使用方法
/*
内部类,外部类中变量重名,使用方法
*/
public class Outer {
int num = 10; // 外部类的成员变量
public class Inner {
int num = 20; // 内部类的成员变量
public void methodInner() {
int num = 30; // 内部类方法的局部变量
System.out.println(num); // 局部变量 30
System.out.println(this.num); // 内部类的成员变量 20
System.out.println(Outer.this.num); // 外部类的成员变量 10
}
}
}
public class Demo02InnerClass {
public static void main(String[] args) {
//创建内部类对象
Outer.Inner o = new Outer().new Inner();
//调用内部类的方法
o.methodInner();//30 20 10
}
}
1.2 内部类的作用
隐藏,可用private、protected修饰
拥有访问外部类的权限
实现多继承(继承一个父类以及自己的外部类)
1.3 内部类的分类
- 成员内部类
- 静态内部类 静态内容不能访问非静态内容
- 局部内部类
- 匿名内部类
2、Lambda
匿名内部类中只有一个抽象方法,简写为Lambda
2.1 无返回值,有参数
public interface Cook {
void makeFood(String name);
}
public static void main(String[] args) {
String name ="高沛";
//使用匿名内部类的方式
Cook c = new Cook() {
@Override
public void makeFood(String name) {
System.out.println(name + ":土豆丝+红烧肉+排骨汤");
}
};
c.makeFood(name);//高沛:土豆丝+红烧肉+排骨汤
//使用Lambda表达式
Cook c1 = (String s)->{ System.out.println(name + ":辣椒炒肉+红烧鸡翅+鸡汤"); };
c1.makeFood(name);//高沛:辣椒炒肉+红烧鸡翅+鸡汤
}
2.2 有返回值,无参数
2.3 无返回值,无参数
集合中用Lambda遍历 forEach()
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
al.add(5);
//Lambda表达式 遍历集合
al.forEach((Integer integer)->{
System.out.println(integer);//1 2 3 4 5
});
}
2.4 有返回值,有参数
3、函数式接口
3.1四大内置核心函数式接口
有参无返回值 Consumer
/*
函数式接口:有参,无返 消费型
*/
public class Demo06FunctionInterface {
public static void main(String[] args) {
spend(1000,(m)->{
System.out.println("您的余额:" + (m-600));;//您的余额:400
});
}
//618 清空购物车后的支付宝余额
public static void spend(Integer money, Consumer<Integer> c){
c.accept(money);
}
}
无参有返回值 Supplier
public class Demo07FunctionInterface {
public static void main(String[] args) {
String l = cook(()->{
String fl = "红烧肉+米饭";
return fl;
});
System.out.println(l);//红烧肉+米饭
}
//餐厅点餐,打印点餐小票
public static String cook(Supplier<String> s){
return s.get();
}
}
有参有返回值 Function<T,R>
public class Demo08FunctionInterface {
public static void main(String[] args) {
String s1 = cook("3号桌:",(s)->{
String fl =s + "红烧肉+米饭";
return fl;
});
System.out.println(s1);//3号桌:红烧肉+米饭
}
//餐厅点餐,打印点餐小票
public static String cook(String str, Function<String,String> r){
return r.apply(str);
}
}
有参有返回值(boolean) Predicate
4、Collections工具类
4.1 常用方法
void sort(List) 对List 容器内的元素排序,按照升序进行排序。
void shuffle(List) 对 List 容器内的元素进行随机排列 void reverse(List) 对 List 容器内的元素进行逆续排列
void fill(List, Object) 用一个特定的对象重写整个List 容器
int binarySearch(List, Object) 采用折半查找的方法查找特定对象
public class Demo09Collections {
public static void main(String[] args) {
//创建集合对象
List<String> l = new ArrayList<>();
//添加元素
l.add("we");
l.add("sfsfssff");
l.add("oudt");
l.add("basdfd");
l.add("nffdffddsfet");
System.out.println(l);//[we, sfsfssff, oudt, basdfd, nffdffddsfet]
//void sort(List) 对List容器内的元素排序,按照升序进行排序。
Collections.sort(l);
System.out.println(l);//[basdfd, nffdffddsfet, oudt, sfsfssff, we]
// int binarySearch(List, Object) 采用折半查找的方法查找特定对象
System.out.println(Collections.binarySearch(l, "sfsfssff"));//3
// void reverse(List) 对List容器内的元素进行逆续排列
Collections.reverse(l);
System.out.println(l);//[we, sfsfssff, oudt, nffdffddsfet, basdfd]
// void shuffle(List) 对List容器内的元素进行随机排列
Collections.shuffle(l);
System.out.println(l);//[nffdffddsfet, we, basdfd, oudt, sfsfssff]
// void fill(List, Object) 用一个特定的对象重写整个 List 容器
Collections.fill(l,"gpgpgp");
System.out.println(l);//[gpgpgp, gpgpgp, gpgpgp, gpgpgp, gpgpgp]
}
}
4.2 Comparable 接口
Comparable是用来被元素类实现的,元素类中指定排序规则,但是一旦指定好了,后面就只能使用这种规则了
//在类中实现接口,并重写抽象方法
@Override
public int compareTo(Student o) {
return this.id-o.id;
}
4.3 Comparator 接口
Comparator是一个比较器,可以在排序的时候指定比较规则
public class Demo10Collections {
public static void main(String[] args) {
//创建集合,添加元素
List<Student> s = new ArrayList<>();
s.add(new Student(01,"张三3"));
s.add(new Student(02,"李四44"));
s.add(new Student(03,"王五555"));
s.add(new Student(04,"赵六6666"));
//使用Comparable,前提是实现接口,重写接口中的抽象方法
Collections.sort(s);
System.out.println(s);//[Student{id=1, name='张三'}, Student{id=2, name='李四'}, Student{id=3, name='王五'}, Student{id=4, name='赵六'}]
//使用Comparator接口,不需要实现接口,但是要手动制定规则
Collections.sort(s,(s1,s2)->{
return -(s1.getName().length()-s2.getName().length());
});
System.out.println(s);//[Student{id=4, name='赵六6666'}, Student{id=3, name='王五555'}, Student{id=2, name='李四44'}, Student{id=1, name='张三3'}]
}
}