匿名类
文章目录
当接口、抽象类的 实现类,在整个项目中只用过一次,可以考虑使用匿名类
public interface Eatable {
String name();
int energy();
}
public class Person {
public void eat(Eatable a) {
System.out.println("eat - " + a.name() + " -" + a.energy());
}
}
public static void main(String[] args) {
Person person = new Person();
person.eat(new Eatable() { // 匿名内部类
@Override
public String name() {
return "Apple";
}
@Override
public int energy() {
return 100;
}
}); // eat - Apple -100
Eatable beef = new Eatable() {
@Override
public String name() {
return "Beef";
}
@Override
public int energy() {
return 200;
}
};
person.eat(beef); // eat - beef -200
}
匿名类01–基本使用
匿名类不能定义除 编译时常量(static int final a = 1
) 以外的任何 static 成员
匿名类只能访问 final
或者 有效final
的局部变量
匿名类可以直接访问外部类中的所有成员(即使被声明为 private)
匿名类只有在实例相关的代码块中使用,才能直接访问外部类中的实例成员(实例变量、实例方法)
匿名类不能自定义构造方法,但可以有初始化块
匿名类02–用途
- 代码传递
- 过滤器
- 回调
public class Times {
public interface Block {
void execute();
}
public static void test(Block block) {
long begin = System.currentTimeMillis();
block.execute();
long end = System.currentTimeMillis();
long duration = end - begin;
System.out.println("花费时间" + duration + "ms");
}
}
public static void main(String[] args) {
Times.test(new Block() {
@Override
public void execute() {
String str = "";
for (int i = 0; i < 10000; i++) {
// 字符串拼接极其耗时间, 因为每次都相当于创建新的对象
str += i;
}
}
});
}
public class Networks {
public interface Block {
void success(Object response);
void failure();
}
public static void get(String url, Block callBack) {
// 1. 根据url发送一个异步请求(开启一条子线程)
// ......
// 2. 请求完毕后
boolean result = url.contains("666") ? true : false;
if (result) {
Object response = null;
callBack.success(response);
} else {
callBack.failure();
}
}
public static void main(String[] args) {
// 传入的网络请求只有满足某些条件时会返回成功, 其他情况会失败
// 但是发送请求时将成功要做的事的和失败要做的事都写好了
// 等到判断条件是否满足以后再回来调用, 这就是回调
Networks.get("http://xxx.com?pwd=666", new Block() {
@Override
public void success(Object response) {
System.out.println("请求成功");
}
@Override
public void failure() {
System.out.println("请求失败");
}
});
}
}
public class Files {
public interface Filter {
boolean accpet(String filename);
}
// 需要传入一个Filter过滤器, 用来自定义要返回满足哪些条件的文件名
public static String[] getAllFileNames(String dir, Filter filter) {
// 1.先获取 dir 文件夹下的所有文件名
String[] allFilesnames = {};
// 2.进行过滤
for (String filename : allFilesnames) {
if (filter.accpet(filename)) {
// 将满足条件的文件名装起来
}
}
// 返回所有装起来的文件名(满足条件的文件名)
return null;
}
public static void main(String[] args) {
// 获取F盘下所有.java文件
Files.getAllFileNames("F:", new Filter() {
@Override
public boolean accpet(String filename) {
if (filename.endsWith(".java")) return true;
return false;
}
});
// 获取F盘下所有名字中带hello的文件
Files.getAllFileNames("F:", new Filter() {
@Override
public boolean accpet(String filename) {
if (filename.contains("hello")) return true;
return false;
}
});
}
}
匿名类03–排序
可以使用 JDK 自带的 java.util.Arrays
类中的sort()
方法对数组进行排序;
Arrays.sort
默认是升序排列,可以通过比较器Comparator
改变次序- 把比较小的元素放左边
- 把比较大的元素放右边
compare
的返回值- 等于 0 :
o1 == o2
- 大于 0 :
o1 > o2
- 小于 0:
o1 < o2
- 等于 0 :
Integer[] array = { 33, 22, 11, 77, 66, 99 };
Arrays.sort(array);
System.out.println(Arrays.toString(array));
// [33, 22, 11, 77, 66, 99]
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
System.out.println(Arrays.toString(array));
// [11, 22, 33, 66, 77, 99]