zoukankan      html  css  js  c++  java
  • 内部类(匿名,非匿名)

    package innerclass;

    /*
    * 方法中的内部类,
    * 1:非匿名内部类
    * 2:匿名内部类
    */
    public class Parcel5 {

    //方法中的内部类,非匿名内部类
    public Destination destination(String s){
    class PDestination implements Destination{
    private String label;
    @Override
    public String readLable() {
    // TODO Auto-generated method stub
    return label;
    }

    public PDestination(String s){
    this.label = s;
    }
    }
    return new PDestination("XX");
    }

    //匿名内部类
    public Contens contens(){
    //匿名内部类
    return new Contens(){
    private int i = 1;
    @Override
    public int value() {
    // TODO Auto-generated method stub
    return i;
    }
    };
    // class PContens implements Contens {
    // private int i = 1;
    // @Override
    // public int value() {
    // // TODO Auto-generated method stub
    // return i;
    // }
    //
    // }
    }

    //匿名的内部类引用外部的参数的时候,外部的参数一定要是final类型的
    public Destination destinationFinal(final String s){
    return new Destination(){
    //private String lable;
    @Override
    public String readLable() {
    return s;
    }
    };
    }

    }

     
    内部类的分类:
    1,成员内部类,2,局部内部类(方法里面的内部类),静态成员内部类,匿名内部类
  • 相关阅读:
    206. 反转链表
    JAVA 排序总结
    Codeforces Round #674 (Div. 3)
    【BM模板】
    【 lca 】最近公共祖先
    【 欧拉函数 】GCD
    【 裴蜀定理 】Border
    【调和级数 && 欧拉常数】 Harmonic Number
    【Lucas定理】组合数取模算法
    【 最短路 && 思维 】Escape Plan
  • 原文地址:https://www.cnblogs.com/working/p/3919860.html
Copyright © 2011-2022 走看看