赞
踩
目录
JDK 8以前的接口:
- interface 接口名 {
- 静态常量;
- 抽象方法;
- }
JDK 8的接口:
- interface 接口名 {
- 静态常量;
- 抽象方法;
- 默认方法;
- 静态方法;
- }
接口引入默认方法的背景
- interface A {
- public abstract void test1();
- // 接口新增抽象方法,所有实现类都需要去重写这个方法,非常不利于接口的扩展
- public abstract void test2();
- }
- class B implements A {
- @Override
- public void test1() {
- System.out.println("BB test1");
- }
- // 接口新增抽象方法,所有实现类都需要去重写这个方法
- @Override
- public void test2() {
- System.out.println("BB test2");
- }
- }
- class C implements A {
- @Override
- public void test1() {
- System.out.println("CC test1");
- }
- // 接口新增抽象方法,所有实现类都需要去重写这个方法
- @Override
- public void test2() {
- System.out.println("CC test2");
- }
- }

- public interface Map<K, V> {
- ...
- abstract void forEach(BiConsumer<? super K, ? super V> action);
- }
- Interface Map<K, V>
- 所有已知实现类:
- AbstractMap,ConcurrentSkipListMap,IdentityHashMap,MapPropertyBase,Provider,Rendering Hints,SimpleMapProperty,Attributes,ReadonlyMapProperty,AuthProvider,EnumMapHashMap,
- clipboardContent,Hashtable,SimpleBindings,TreeMap,ConcurrentHashMap,MapProperty,
- ReadOnlyMapWrapper,Headers.Properties,LinkedHashMap,MultiMap Result,ScriptObjectMirror,
- TabularDataSupport,MapBinding,MapExpression,PrinterStateReasons,ReadonlyMapPropertyBase,UIDefaults,WeakHashMap
-
- public interface Map<K, V> {
- ...
- default void forEach(BiConsumer<? super K, ? super V> action) {
- ...
- }
- }
接口默认方法的定义格式
- interface 接口名 {
- 修饰符 default 返回值类型 方法名() {
- 代码;
- }
- }
接口默认方法的使用
- public class Demo02UserDefaultFunction {
- public static void main(String[] args) {
- BB b = new BB();
- // 方式一:实现类直接调用接口默认方法
- b.test02();
- CC c = new CC();
- // 调用实现类重写接口默认方法
- c.test02();
- }
- }
- interface AA {
- public abstract void test1();
- public default void test02() {
- System.out.println("AA test02");
- }
- }
- class BB implements AA {
- @Override
- public void test1() {
- System.out.println("BB test1");
- }
- }
- class CC implements AA {
- @Override
- public void test1() {
- System.out.println("CC test1");
- }
- // 方式二:实现类重写接口默认方法
- @Override
- public void test02() {
- System.out.println("CC实现类重写接口默认方法");
- }
- }

接口静态方法的定义格式
- interface 接口名 {
- 修饰符 static 返回值类型 方法名() {
- 代码;
- }
- }
接口静态方法的使用
- public class Demo04UseStaticFunction {
- public static void main(String[] args) {
- // 直接使用接口名调用即可:接口名.静态方法名();
- AAA.test01();
- }
- }
- interface AAA {
- public static void test01() {
- System.out.println("AAA 接口的静态方法");
- }
- }
- class BBB implements AAA {
- /* @Override 静态方法不能重写
- public static void test01() {
- System.out.println("AAA 接口的静态方法");
- }*/
- }

内置函数式接口来由来
- import java.util.List;
- public class Demo01UserFunctionalInterface {
- public static void main(String[] args) {
- // 调用函数式接口中的方法
- method((arr) -> {
- int sum = 0;
- for (int n : arr) {
- sum += n;
- }
- return sum;
- });
- }
- // 使用自定义的函数式接口作为方法参数
- public static void method(Operator op) {
- int[] arr = {1, 2, 3, 4};
- int sum = op.getSum(arr);
- System.out.println("sum = " + sum);
- }
- }
- @FunctionalInterface
- interface Operator {
- public abstract int getSum(int[] arr);
- }

常用内置函数式接口介绍
- @FunctionalInterface
- public interface Supplier<T> {
- public abstract T get();
- }
- @FunctionalInterface
- public interface Consumer<T> {
- public abstract void accept(T t);
- }
- @FunctionalInterface
- public interface Function<T, R> {
- public abstract R apply(T t);
- }
- @FunctionalInterface
- public interface Predicate<T> {
- public abstract boolean test(T t);
- }
- Predicate接口用于做判断,返回boolean类型的值
- @FunctionalInterface
- public interface Supplier<T> {
- public abstract T get();
- }
- public class Demo05Supplier {
- public static void main(String[] args) {
- printMax(() -> {
- int[] arr = {10, 20, 100, 30, 40, 50};
- // 先排序,最后就是最大的
- Arrays.sort(arr);
- return arr[arr.length - 1]; // 最后就是最大的
- });
- }
- private static void printMax(Supplier<Integer> supplier) {
- int max = supplier.get();
- System.out.println("max = " + max);
- }
- }
- @FunctionalInterface
- public interface Consumer<T> {
- public abstract void accept(T t);
- }
- import java.util.function.Consumer;
- public class Demo06Consumer {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- System.out.println(s.toLowerCase());
- });
- }
- public static void test(Consumer<String> consumer) {
- consumer.accept("HelloWorld");
- }
- }
- default Consumer<T> andThen(Consumer<? super T> after) {
- Objects.requireNonNull(after);
- return (T t) -> { accept(t); after.accept(t); };
- }
- public class Demo07ConsumerAndThen {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- System.out.println(s.toLowerCase());
- }, (String s) -> {
- System.out.println(s.toUpperCase());
- });
- // Lambda表达式简写
- test(s -> System.out.println(s.toLowerCase()), s ->
- System.out.println(s.toUpperCase()));
- }
- public static void test(Consumer<String> c1, Consumer<String > c2) {
- String str = "Hello World";
- // c1.accept(str); // 转小写
- // c2.accept(str); // 转大写
- // c1.andThen(c2).accept(str);
- c2.andThen(c1).accept(str);
- }
- }

- @FunctionalInterface
- public interface Function<T, R> {
- public abstract R apply(T t);
- }
- public class Demo08Function {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- return Integer.parseInt(s); // 10
- });
- }
- public static void test(Function<String, Integer> function) {
- Integer in = function.apply("10");
- System.out.println("in: " + (in + 5));
- }
- }
- default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
- Objects.requireNonNull(after);
- return (T t) -> after.apply(apply(t));
- }
- public class Demo09FunctionAndThen {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- return Integer.parseInt(s);
- }, (Integer i) -> {
- return i * 10;
- });
- }
- public static void test(Function<String, Integer> f1, Function<Integer, Integer> f2) {
- // Integer in = f1.apply("66"); // 将字符串解析成为int数字
- // Integer in2 = f2.apply(in);// 将上一步的int数字乘以10
- Integer in3 = f1.andThen(f2).apply("66");
- System.out.println("in3: " + in3); // 660
- }
- }

- @FunctionalInterface
- public interface Predicate<T> {
- public abstract boolean test(T t);
- }
- Predicate接口用于做判断,返回boolean类型的值
- public class Demo10Predicate {
- public static void main(String[] args) {
- test(s -> s.length() > 3, "迪丽热巴");
- }
- private static void test(Predicate<String> predicate, String str) {
- boolean veryLong = predicate.test(str);
- System.out.println("名字很长吗:" + veryLong);
- }
- }
- default Predicate<T> and(Predicate<? super T> other) {
- Objects.requireNonNull(other);
- return (t) -> test(t) && other.test(t);
- }
- public class Demo10Predicate_And_Or_Negate {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- return s.contains("H");
- }, (String s) -> {
- return s.contains("W");
- });
- }
- public static void test(Predicate<String> p1, Predicate<String> p2) {
- String str = "HelloWorld";
- boolean b1 = p1.test(str); // 判断包含大写“H”
- boolean b2 = p2.test(str); // 判断包含大写“W”
- // if (b1 && b2) {
- // System.out.println("即包含W,也包含H");
- // }
- boolean bb = p1.and(p2).test(str);
- if (bb) {
- System.out.println("即包含W,也包含H");
- }
- }
- }

- default Predicate<T> or(Predicate<? super T> other) {
- Objects.requireNonNull(other);
- return (t) -> test(t) || other.test(t);
- }
- public class Demo10Predicate_And_Or_Negate {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- return s.contains("H");
- }, (String s) -> {
- return s.contains("W");
- });
- }
- public static void test(Predicate<String> p1, Predicate<String> p2) {
- String str = "HelloWorld";
- boolean b1 = p1.test(str); // 判断包含大写“H”
- boolean b2 = p2.test(str); // 判断包含大写“W”
- // if (b1 || b2) {
- // System.out.println("有H,或者W");
- // }
- boolean bbb = p1.or(p2).test(str);
- if (bbb) {
- System.out.println("有H,或者W");
- }
- }
- }

- default Predicate<T> negate() {
- return (t) -> !test(t);
- }
- import java.util.function.Predicate;
- public class Demo10Predicate_And_Or_Negate {
- public static void main(String[] args) {
- // Lambda表达式
- test((String s) -> {
- return s.contains("H");
- }, (String s) -> {
- return s.contains("W");
- });
- }
- public static void test(Predicate<String> p1, Predicate<String> p2) {
- String str = "HelloWorld";
- boolean b1 = p1.test(str); // 判断包含大写“H”
- boolean b2 = p2.test(str); // 判断包含大写“W”
- // 没有H,就打印
- // if (!b1) {
- // System.out.println("没有H");
- // }
- boolean test = p1.negate().test(str);
- if (test) {
- System.out.println("没有H");
- }
- }
- }

- @FunctionalInterface
- public interface Supplier<T> {
- public abstract T get();
- }
- @FunctionalInterface
- public interface Consumer<T> {
- public abstract void accept(T t);
- }
- @FunctionalInterface
- public interface Function<T, R> {
- public abstract R apply(T t);
- }
- @FunctionalInterface
- public interface Predicate<T> {
- public abstract boolean test(T t);
- }
- Predicate接口用于做判断,返回boolean类型的值
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。