面向对象编程(OOP)
首先Java面向对象编程有三大特性为封装性、继承、多态
- 封装:决定数据访问的限制
- 继承:决定上下游的关系
- 多态:一种接口多种实现
面向切面编程(AOP)
面向切面编程分为三种种,一种静态、一种动态、字节码提升
- 静态接口:使用java.util.logging.Filter、java.util.EventListener,当我执行代码的时候会进行拦截通过规定拦截方法、注解、参数、异常的方式,会有事件进行回调的过程可以在前置、后置、返回、异常中进行执行
- 动态代理:也是从字节码提升中来,通过java.lang.reflect.Proxy和java.lang.reflect.InvocationHandler中来实现动态代理
- 字节码提升:使用ASM(ObjectWeb)、CGLIB、Javassist(JBoss)、BCEL(Apache)
面向元信息编程(MDOP)
元数据编程分为:泛型、反射、注解
- 泛型:
public interface List<E> extends Collection<E>
- 注解:使用@Annotation
- 反射:Relection 通过反射可以获取运行时的信息
- java.lang.reflect.Member
- java.lang.reflect.AnnotateElement
- java.lang.reflect.Executable
- java.lang.reflect.Field
- java.lang.reflect.Method
- java.lang.reflect.Constructor
面向函数编程(FOP)
函数式接口:一般可以使用函数式编程的接口上都会有@FunctionalInterface
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface{
}
@FunctionalInterface
public interface Consumer<T>{
...
void accept(T t);
...
}
默认方法:在接口上提供一个 default 的方法
public interface Function<T,R>{
...
default <V> Function<V,R> compose(Function<? super V,? extends T> before){
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
}
方法引用
public final class Collectors{
...
public static <T> Collector<T, ?, Long> counting(){
return reducting(0L,e ->1L,Long::sum);
}
}
面向模块编程(MOP):将最小粒度设定为模块,在使用的时候可以导入具体的模块进行使用,主要提高可靠性和封装性,提高系统同的健壮性
module com.foo.bar {
requires org.baz.qux;//一个或更多个requires项可以被添加到其中,它通过名字声明了这个模块依赖的一些其他模块,在编译期和运行期都依赖的。
exports com.foo.bar.alpha;//exports项可以被添加,它可以仅仅使指定包(package)中的公共类型可以被其他的模块使用。
}