- (被重写方法的形参列表)->{
- 被重写方法的方法体代码。
- }
- package com.example.Test;
- public class draft {
- public static void main(String[] args) {
- // 创建匿名内部类对象
- Animal dog = new Animal() {
- @Override
- public void run() {
- System.out.println("狗跑的贼快");
- }
- };
- dog.run();
- }
-
- }
-
- interface Animal {
- void run();
- }
使用Lambda表达式
- package com.example.Test;
-
- public class draft {
- public static void main(String[] args) {
- Animal dog = () -> {
- System.out.println("狗跑的贼快");
- };
- dog.run();
- }
-
- }
-
- interface Animal {
- void run();
- }
运行结果如下
