内部类(匿名,非匿名)

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,局部内部类(方法里面的内部类),静态成员内部类,匿名内部类
原文地址:https://www.cnblogs.com/working/p/3919860.html