Inner class, partial classes, anonymous classes

1.Java file inside the class as an inner class.
Method 2.Java inside the class is a partial class.
Local class do not need to add the public modifier, because the method executed, class disappears

public class Person(){
private int age;
public void run(){
class Run{
		}
	}
}

Run class is a partial internal class
definitions partial inner class defined as local variables, without qualifier, which is a scope of the present code block.
3. anonymous class that is not the name of the class, the name given by the Java compiler, not a reference can not be instantiated, can only be used once, of course, can not have a constructor.

 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类型");
            }
        });

Guess you like

Origin blog.csdn.net/weixin_44084434/article/details/91393740