java项目总结8

1.方法引用

1.方法引用概述

注意注意:

1.引用出必须是函数式接口

2.被引用的方法必须已经存在

3.被引用方法的型参和返回值需要跟抽象方法保持一致

4.被引方法的功能要满足当前需求

Arrays.sort(arr,Main::subtraction);

Main是该类的名称,::是方法引用符

     //方法引用
        Integer[] arr={4,3,1,6,2,7,8,5};
        Arrays.sort(arr,run1::subtraction);
        System.out.println(Arrays.toString(arr));
    }
 
    public static int subtraction(int num1,int num2){
        return num2-num1;
    }
 
}

2.引用静态方法

1.方法需要已经存在
2.方法的形参和返回值需要跟抽象方法的形参和返回值保持一致

3.方法的功能需要把形参的字符串转换成整数

未利用方法引用

方法引用如下:引用方法parseInt

    ArrayList<String> list10 = new ArrayList<>();
        Collections.addAll(list10, "1","4","3","2","6");
        list10.stream()
                .map(Integer::parseInt)
                .forEach(s->System.out.println(s));

3.引用成员方法

格式:对象::成员方法

1.其他类:其他类对象::方法名

2.本类:this::方法名(引用出处不能是父类方法)

3.父类:super::方法名

1.引用其他成员方法

另一个类所定义的方法如/*线以下所示

第五行使用时候需要(对象,方法);这里的对象是在另一个类,所有需要new erer();

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");
 
        list1.stream().filter(new erer()::stringpd)
                .forEach(s -> System.out.println(s));
    }
 }
 
/*      另一个类的方法如下
 
public class erer {
    public boolean stringpd(String s){
        return s.startsWith("张")&&s.length()==3;
    }
}
 */

2.引用本类成员方法

注意:静态方法无this(所以要用本类的对象new Main())

​
public class Main {
    public static void main(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");
 
        list1.stream().filter(new Main()::stringpd)
                .forEach(s -> System.out.println(s));
    }
 }
 
 
public class erer {
    public boolean stringpd(String s){
        return s.startsWith("张")&&s.length()==3;
    }
}


​

3.引用父类成员方法

拓展:(挺难的)

4.引用构造方法

稿子或者前提:

未引用构造方法:

//匿名内部类    
    List<Student> newlist1 = list1.stream().map(new Function<String, Student>() {
            @Override
            public Student apply(String s) {
                String[] arr = s.split(",");
                String name = arr[0];
                int age = Integer.parseInt(arr[1]);
                return new Student(name, age);
            }
        }).collect(Collectors.toList());
 
        //lambda
        List<Student> new2list1 = list1.stream()
                .map(s -> new Student(s.split(",")[0]
                        , Integer.parseInt(s.split(",")[1])))
                .collect(Collectors.toList());

这里要修改Student的录入方式,将(String name,int age)变为(String str)name和age在str中获得。

     //引用构造方法
        List<Student> new3list1 = list1.stream().map(Student::new).collect(Collectors.toList());
        System.out.println(new3list1);
 
 
//其中Strudent这个类里面要加上
  public Student(String str) {
        String[] arr = str.split(",");
        this.name = arr[0];
      this.age = Integer.parseInt(arr[1]);
    }

引用构造方法:

5.其他调用方式

1.使用类名引用成员方式

格式:类名::成员方法

范例:String::substring

特有规则:

所以下面才成立:

注意:第一个参数决定了我能引用那个类中的方法。如上图第一个参数是String,引用的类是String。

2.引用数组的构造方式

格式:数据类型[ ]::new

范例:int[ ]::new

细节:数组的类型,需要跟流中数据的类型保持一致。

总结:

6.实战演练

都挺难的!!!

1.转成自定义对象并收集到数组

如下:

Student[] arr的那一行中的第一个Student::new是指引用构造方法,Student[]是指引用数组的构造方法

2.获取部分属性并收集到数组

如下:

技巧:

2.异常

1.异常体系介绍

异常:程序中出现的问题

2.编译时异常和运行时异常

3.异常的作用

4.JVM虚拟机默认处理异常的方式

5.Try..... catch捕获异常

6.捕获异常灵魂四问

通常写在方法调用处

1.

2.

3.

4.

小结:

7.异常中的常见方法

Alt+Ctel+t快速建立try并指定有可能的错误

8.抛出异常

(通常写在方法里)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值