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

匿名对象
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值