1.Java文件里面的类为内部类。
2.Java方法里面的类为局部类。
局部类不需要加public修饰符,因为方法执行完,类就消失了
public class Person(){
private int age;
public void run(){
class Run{
}
}
}
类Run就是一个局部内部类
局部内部类的定义与局部变量的定义一样,不加限定符,其作用域是本代码块。
3.匿名类即没有名字的类,其名称由Java编译器给出,不能引用不能实例化,只能用一次,当然也不能有构造器。
new Date(){};
Collections.sort(s, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Student && o2 instanceof Student){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if(s1.getCode() > s2.getCode()) return 1;
if(s1.getCode() < s2.getCode()) return -1;
return 0;
}
throw new RuntimeException("必须是Student类型");
}
});