一.什么是内部类
1.内部类是外部类中的元素
二.内部类的定义
1.外部类中使用内部类,按正常的方式使用
2.外部类以外使用非静态内部类
Peop peop = new Peop();
Peop.Mess mess = peop.new Mess();
mess.getChinese();
public class Peop {
private String name;
private Mess mess;
public void initMess(){
mess = new Mess();
}
public class Mess {
public Mess() {
}
private String math;
private String english;
private String chinese;
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getChinese() {
return chinese;
}
public void setChinese(String chinese) {
this.chinese = chinese;
}
}
;
}
3.外部类以外使用静态内部类
Robot.ContentBuilder builder = new Robot.ContentBuilder();
builder.getHigh();
class Robot {
static class ContentBuilder {
private String high;
private String width;
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
}
}
4.局部内部类
class Rectangle {
private int width;
private int height;
public int getArea() {
class Rule {
int measureWidth = 3;
int measureHeight = 4;
public int getRule() {
return measureHeight * measureWidth;
}
}
Rule rule = new Rule();
return rule.getRule();
}
}
5.匿名内部类
new 接口()|父类构造器(实参列表){
//匿名内部类类体部分
}
new BaseView(){
@Override
public void showView() {
}
};
interface BaseView {
void showView();
}

被折叠的 条评论
为什么被折叠?



