java8学习笔记

java8

java annotation (注解)

  • @Override 覆写
  • @Deprecated 过期
  • @SuppressWarnings({ “rawtypes”, “unchecked” }) 压制警告

Java 泛型

通配符

  • Message<?>: 泛型类型通配
  • Message<? extends 类>: 设置泛型上限,子类
  • Message<? super 类>: 设置泛型下限,父类

泛型接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface IMessage<T> { //设置泛型接口
public void print(T t);
}
class MessageImpl<T> implements IMessage<T> {
public void print(T t) {
System.out.println(t);
}
}
public class TestDemo {
public static void main(String[] args){
IMessage<String> msg = new MessageImpl<String>();
msg.print("Hello World!");
}
}

泛型方法

1
2
3
public static <T> T fun(T t) { //T的类型由传入类型决定
return t;
}

枚举

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
enum Color { //相当于一个类继承了Enum类(抽象类)而已
RED,GREEN,BLUE;
}
public class TestDemo {
public static void main(String[] args){
Color red = Color.RED;
System.out.println(red);
for (Color c : Color.values()) { //values()
System.out.println(c.ordinal() + " - ");
}
}
}
//扩充枚举功能
enum Color { //相当于一个类继承了Enum类(抽象类)而已
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
private Color(String title) {
this.title = title;
}
}
public class TestDemo {
public static void main(String[] args){
Color red = Color.RED;
System.out.println(red);
for (Color c : Color.values()) { //values()
System.out.println(c); //调用toString()
}
}
}
//枚举实现接口
enum Color implements Message {
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
private Color(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
public class TestDemo {
public static void main(String[] args){
Message msg = Color.RED;
System.out.println(msg.getTitle());
}
}
//枚举最变态的做法
enum Color implements Message {
RED("红色"){
public String getTitle() {
return "自己的" + this.title;
}
},GREEN("绿色"),BLUE("蓝色");
private String title;
private Color(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}

泛型中定义普通方法

使用default在接口中定义普通方法 使用static在接口中定义普通方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface IMessage {
public void print();
default void fun() { //此处定义了一个普通方法
System.out.println("毁三观的方法");
}
static void get() {
System.out.println("直接由接口调用!");
}
}
class MessageImpl implements IMessage {
@Override
public void print() {
System.out.println("Hello World!");
}
}
public class TestDemo {
public static void main(String[] args){
IMessage msg = new MessageImpl();
msg.fun(); //接口中直接定义的
}
IMessage.get();
}

lambda表达式

匿名内部类

1
2
3
4
5
6
7
8
9
10
11
12
public class TestDemo {
public static void main(String[] args){
fun(new IMessage() {
public void print() {
System.out.println("Hello World!!!");
}
});
}
}
// lambda
fun( () -> System.out.println("Hello World!!!") );

lambda

  • (参数表)-> 单行语句;
  • (参数表)-> {单行语句};
  • (参数表)-> 表达式;
1
2
3
4
5
6
7
8
9
10
11
12
interface IMessage {
public void print(String str);
}
public class TestDemo {
public static void main(String[] args){
fun((s) -> System.out.println(s));
}
public static void fun(IMessage msg) {
msg.print("Hello World!");
}
}

4种方法引用

  • 引用静态方法: 类名称::static 方法名称;
  • 引用某个对象的方法: 实例化对象::普通方法;
  • 引用特定类型的方法: 特定类::普通方法;
  • 引用构造方法: 类名称::new;
1
2
3
4
5
6
7
8
9
10
11
12
@FunctionalInterface //此注解指定该接口内部只存在一个方法,即函数式接口(可以引用函数)
interface IMessage<P,R> {
public R zhuanhuan(P p);
}
public class TestDemo {
public static void main(String[] args){
//引用类的普通方法
IMessage<Integer,String> msg = String::valueOf;
String str = msg.zhuanhuan(1000);
System.out.println(str.replaceAll("0", "9"));
}
}
时光不停 TeslaChan @buting
转载请注明出处!