1.内外部类都是非static方式
//外部类
public class OutClass {
//内部类
public class Inner{
//内部类的方法
public int innermethodone(){
return 1;
}
public int innermethodtwo(){
return 2;
}
}
//成员方法
public void outMethod(){
}
}
访问内部类的method
Integer result = new OutClass().new Inner().innermethodone();
2.内部类是static
//外部类
public class OutClass {
//内部类
public static class Inner{
//内部类的方法
public int innermethodone(){
return 1;
}
public int innermethodtwo(){
return 2;
}
}
//成员方法
public void outMethod(){
}
}
Integer result = new OutClass.Inner().innermethodone();
例如Android里面的Builder定义
Request request = new Request.Builder().url(path).build();
他之所以能在方法继续使用方法的原因是url()方法返回的是Builder,所以还能调用build()
我们可以实现相同的方法
//外部类
public class OutClass {
//内部类
public static class Inner{
Object args;
public Inner(){
}
public Inner(Object args){
this.args = args;
}
//内部类的方法
public Inner innermethodone(Object args){
args = args;
return this;
}
public int innermethodtwo(){
return 2;
}
}
//成员方法
public void outMethod(){
}
}
Integer integer = new OutClass.Inner().innermethodone("hello").innermethodtwo();
网友评论