匿名对象、内部类、静态代码块

匿名对象
public class School {
    //静态常量
    //一旦赋值终身不变
    //类名.变量名调用
    public static final String SCHOOL_NAME="清华大学";
    int age;
}

public class Demo {
    public static void main(String[] args) {
        System.out.println(School.SCHOOL_NAME);//类名.变量名调用
        new School();
        System.out.println(new School().age);
        show(new School());
    }
    public static void show(School s){
        System.out.println("我学校的年龄是"+s.age);
    }
}
内部类
//外部类
public class Outer {
    private int i=0;
    //成员内部类
    class Inner{
        int i=2;
        public void show(){
            int i=1;
            System.out.println(Outer.this.i);
        }
    }
}
//外部类
public class Wai {
    public void show(){
        //局部内部类
        class Nei{
            public void show1(){
                System.out.println("这是局部内部类的方法");
            }
        }
        Nei in=new Nei();
        in.show1();
    }
}
public class Test {
    public static void main(String[] args) {
        //外部类类名.内部类类名  变量名=new 外部类类名().new 内部类类名();
        Outer.Inner oi=new Outer().new Inner();
        oi.show();//0
        Wai w=new Wai();
        w.show();//这是局部内部类的方法
    }
}
静态代码块
public class Person {
    public static void main(String[] args) {
        Person p=new Person();
    }
    static{
        System.out.println("这是静态代码块");
    }
}
public interface Smoke {
    public abstract void smoke();
}



public class Test {
    public static void main(String[] args) {
        //匿名内部类对象
        //new 父类(){
        //重写父类的方法
        //}
        new Smoke(){
            public void smoke() {
                System.out.println("抽烟");
            }
        }.smoke();
    }
}
原文地址:https://www.cnblogs.com/zhaotao11/p/10218689.html