接口外部类实现,内部静态类实现,局部类实现,匿名类实现 以及,函数式编程
import java.lang.reflect.Method;
public class b {
static class class2 implements Interface1 {
public void method1() {
System.out.println();
System.out.println("这是内部静态类的示例");
}
}
public static void main(String[] args) {
// ********************************/
// 外部类class1,实现外部接口示例
// ********************************/
class1 test01 = new class1();
test01.method1();
test01.function();
// ********************************/
// 内部静态类class2,实现外部接口示例
// ********************************/
class2 test02 = new class2();
test02.method1();
// ********************************/
// 局部类class3,实现外部接口示例
// ********************************/
class class3 implements Interface1 {
public void method1() {
System.out.println();
System.out.println("这是局部内部类的示例");
}
}
class3 test03 = new class3();
test03.method1();
// 难点
// ********************************/
// 匿名内部类:没有类名,但是有接口名
// 父子继承特性
// 直接重写接口构造方法
// ********************************/
// 父子继承特性,且都可以使用interface1中的接口
Interface1 test04 = new class1();
test04 = new class2();
test04 = new class3();
// 我不通过类来实现了,我直接重写interface1的构造方法,再重写method方法
// 重写构造方法,在重写的构造方法中重写method方法
test04 = new Interface1() {
@Override
public void method1() {
System.out.println();
System.out.println("这里重写了 interface1的构造方法");
}
};
test04.method1();
// 函数式编程:
// 在外面写好接口(先定义),再回到内部实现接口,使用接口(后使用)
// 对上例的方法进行简化,因为接口中只有一个函数,所以函数名也可以省略,让程序自动去推测重写的方法
test04 = () -> {
System.out.println();
System.out.println("对直接重写构造和方法简化");
};
test04.method1();
}
}
// 这是一个外部接口,本示例必须要用接口
interface Interface1 {
void method1();
}
class class1 implements Interface1 {
public void function() {
System.out.println("这是一个外部类");
};
public void method1() {
System.out.println("这是,外部类实现接口的示例");
}
}
函数式编程
import java.nio.channels.FileLockInterruptionException;
/
// 在c/c中是不是需要先定义后使用?
// 这里就是对函数式编程的"先定义"
/
interface f1 {
void function(int a);
}
interface f2 {
void function();
}
interface f3 {
void function();
}
interface f4 {
void function(int a,int b);
}
public class b {
public static void main(String[] args) {
f1 test01 = a -> {
System.out.println("这就是函数式编程,函数的实现: 带参"+a);
};
test01.function(10);
f3 test02 = () -> {
System.out.println("函数式编程,不带参");
};
test02.function();
f2 test03 = () -> System.out.println("再次简化,但是只能执行一条语句;需要执行多语句必须用{}来划分语句块");System.out.println("这行语句不执行");
test03.function();
f4 test04 = (a,b)->{
System.out.println("函数式编程,多参数示例")
}
}
}