- 内部类可以用多个实例,每个实例都有自己的状态信息,并且与其他外围对象的信息相互独立。
- 在单个外围类中,可以让多个内部类以不同的方式实现同一个接口,或者继承同一个类。
- 创建内部类对象的时刻并不依赖于外围类对象的创建。
- 内部类并没有令人迷惑的“is-a”关系,他就是一个独立的实体。
- 内部类提供了更好的封装,除了该外围类,其他类都不能访问。
1)静态内部类:
必须以static关键字标注
只能访问外部类中的静态的成员变量或者是静态的方法
访问一个内部类使应该这样outerClass.innerClass inter = new outerClass.innerClass();不能直接实例化内部类
2)成员内部类:
定义在一个类的内部,但是没有static关键字修饰
生成示例的方法outerClass.innerClass inter = (new outerClass()).new innerClass()
对外部类变量的引用outClass.this.variale
可以访问外部类的静态与非静态方法
3)局部内部类:
局部内部类指的是定义在一个方法中的类
只有在当前方法中才能对局部内部类里面的方法以及变量进行访问
局部内部类只能访问其所在方法的final类型变量
4)匿名内部类:
隐式的继承一个父类或者是实现某个接口
成员内部类
//成员内部类
public class TalkingClock {
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep){
this.interval = interval;
this.beep = beep;
}
public void start(){
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval,listener);
t.start();
}
public class TimePrinter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("At the tone, the time is " + new Date());
if(beep&&interval==1000) { //TalkingClock.this.beep
Toolkit.getDefaultToolkit().beep();
}
}
}
}
//反射
public class.TalkingClock {
private int interval;
private boolean beep;
public com.TalkingClock(int, boolean);
public void start();
static boolean access$0(com.TalkingClock); //外围类添加静态方法,静态方法名字由编译器决定
static int access$1(com.TalkingClock);
}
public class com.TalkingClock$TimePrinter implements java.awt.event.ActionListener {
final com.TalkingClock this$0; //引用外围类,this$0 是由编译器合成的
public com.TalkingClock$TimePrinter(com.TalkingClock);
public void actionPerformed(java.awt.event.ActionEvent);
}
//反编译
public class TimePrinter implements ActionListener
{
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("At the tone, the time is " + new Date());
if (TalkingClock.access$0(TalkingClock.this) && TalkingClock.access$1(TalkingClock.this) == 1000) {
Toolkit.getDefaultToolkit().beep();
}
}
}
成员内部类2
//成员内部类2
public class A {
private String name;
private int age;
public A() {
name = "周杰伦";
age = 20;
new B();
}
public void method1(){
}
public String method2(String str){
return str;
}
class B {
private String name;
private int age;
public B() {
name = "林俊杰";
age = 18;
method1();
method2("Hello World!");
new C();
}
class C {
private String name;
private int age;
public C() {
name = "李沁";
age = 16;
new D();
}
class D {
private String name;
private int age;
public D() {
name = "王源";
age = 14;
method1();
method2("Hello World");
print();
}
public void print() {
System.out.println(this.name + " " + this.age);
System.out.println(C.this.name + " " + C.this.age);
System.out.println(B.this.name + " " + B.this.age);
System.out.println(A.this.name + " " + A.this.age);
}
}
}
}
}
//反射
//A类
public class com.A {
private java.lang.String name;
private int age;
public com.A(); //A类构造方法
public void method1(); //A类method1方法
public java.lang.String method2(java.lang.String); //A类method2方法
static java.lang.String access$0(com.A); //A类自动生成静态方法,由B类调用获取A类的name域
static int access$1(com.A); //A类自动生成静态方法,由B类调用获取A类的age域
}
//B类
class com.A$B {
private java.lang.String name;
private int age;
final com.A this$0; //A类实例
public com.A$B(com.A); //B类自动生成构造方法传入A类实例
static java.lang.String access$0(com.A$B); //B类自动生成静态方法,由C类调用获取B类的name域
static int access$1(com.A$B); //B类自动生成静态方法,由C类调用获取B类的age域
static com.A access$2(com.A$B); //B类自动生成静态方法,由C类调用获取A类的实例
}
//C类
class com.A$B$C {
private java.lang.String name;
private int age;
final com.A$B this$1; //B类实例
public com.A$B$C(com.A$B); //C类自动生成构造方法传入B类实例
static java.lang.String access$0(com.A$B$C); //C类自动生成静态方法,由D类调用获取C类的name域
static int access$1(com.A$B$C); //C类自动生成静态方法,由D类调用获取C类的age域
static com.A$B access$2(com.A$B$C); //C类自动生成静态方法,由D类调用获取B类的实例
}
//D类
class com.A$B$C$D {
private java.lang.String name;
private int age;
final com.A$B$C this$2; //C类实例
public com.A$B$C$D(com.A$B$C); //D类自动生成构造方法传入B类实例
public void print(); //D类print方法
}
A.this.method1(); //因为B类拥有A类的实例域,所以直接用实例A.this调用方法
A.this.method2("Hello World!");
B.access$2(C.access$2(C.this)).method1();
B.access$2(C.access$2(C.this)).method2("Hello World");
this.name this.age
C.access$0(C.this) C.access$1(C.this)
B.access$0(C.access$2(C.this)) B.access$1(C.access$2(C.this))
A.access$0(B.access$2(C.access$2(C.this))) A.access$1(B.access$2(C.access$2(C.this)));
//反编译
// Decompiled by Procyon v0.5.29
public class A {
private String name;
private int age;
public A() {
this.name = "\u5468\u6770\u4f26";
this.age = 20;
new B();
}
public void method1() {
}
public String method2(final String str) {
return str;
}
class B {
private String name;
private int age;
final /* synthetic */ A this$0;
public B() {
this.name = "\u6797\u4fca\u6770";
this.age = 18;
A.this.method1();
A.this.method2("Hello World!");
new C();
}
class C {
private String name;
private int age;
final /* synthetic */ B this$1;
public C() {
this.name = "\u674e\u6c81";
this.age = 16;
new D();
}
public D() {
this.name = "\u738b\u6e90";
this.age = 14;
B.access$2(C.access$2(C.this)).method1();
B.access$2(C.access$2(C.this)).method2("Hello World");
this.print();
}
public void print() {
System.out.println(String.valueOf(this.name) + " " + this.age);
System.out.println(String.valueOf(C.access$0(C.this)) + " " + C.access$1(C.this));
System.out
.println(String.valueOf(B.access$0(C.access$2(C.this))) + " " + B.access$1(C.access$2(C.this)));
System.out.println(String.valueOf(A.access$0(B.access$2(C.access$2(C.this)))) + " "
+ A.access$1(B.access$2(C.access$2(C.this))));
}
}
}
}
局部内部类
//局部内部类代码
public class TalkingClock {
private int num1 = 3;
private static int num2 = 4;
public static void main(String[] args) {
// TODO Auto-generated method stub
TalkingClock t = new TalkingClock();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(int interval, boolean beep){
class TimePrinter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("At the tone, the time is " + new Date());
if(beep&&interval==1000) { //TalkingClock.this.beep
Toolkit.getDefaultToolkit().beep();
}
System.out.println(num1);
System.out.println(num2);
System.out.println(num1 = 5);
System.out.println(num2 = 6);
}
}
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval,listener);
t.start();
}
}
//反射
public class com.TalkingClock {
private int num1;
private static int num2;
static {};
public com.TalkingClock();
public static void main(java.lang.String[]);
public void start(int, boolean);
static int access$0(com.jubu.TalkingClock);
static int access$1();
static void access$2(com.jubu.TalkingClock, int);
static void access$3(int);
}
class com.TalkingClock$1TimePrinter implements java.awt.event.ActionListener {
final com.TalkingClock this$0;
private final boolean val$beep; //局部变量的备份
private final int val$interval; //局部变量的备份
com.jubu.TalkingClock$1TimePrinter(com.jubu.TalkingClock, boolean, int);
public void actionPerformed(java.awt.event.ActionEvent);
}
//反编译
public class TalkingClock
{
private int num1;
private static int num2;
static {
TalkingClock.num2 = 4;
}
public TalkingClock() {
this.num1 = 3;
}
public static void main(final String[] args) {
final TalkingClock t = new TalkingClock();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(final int interval, final boolean beep) {
class TimePrinter implements ActionListener
{
private final /* synthetic */ boolean val$beep;
private final /* synthetic */ int val$interval;
TimePrinter(final boolean val$beep, final int val$interval) {
this.val$beep = val$beep;
this.val$interval = val$interval;
}
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("At the tone, the time is " + new Date());
if (this.val$beep && this.val$interval == 1000) {
Toolkit.getDefaultToolkit().beep();
}
System.out.println(TalkingClock.access$0(TalkingClock.this));
System.out.println(TalkingClock.access$1());
final PrintStream out = System.out;
final TalkingClock this$0 = TalkingClock.this;
final int n = 5;
TalkingClock.access$2(this$0, n);
out.println(n);
final PrintStream out2 = System.out;
final int n2 = 6;
TalkingClock.access$3(n2);
out2.println(n2);
}
}
final ActionListener listener = new TimePrinter();
final Timer t = new Timer(interval, listener);
t.start();
}
static /* synthetic */ void access$2(final TalkingClock talkingClock, final int num1) {
talkingClock.num1 = num1;
}
static /* synthetic */ void access$3(final int num2) {
TalkingClock.num2 = num2;
}
}
匿名内部类
//匿名内部类代码
public class TalkingClock {
public static void main(String[] args) {
// TODO Auto-generated method stub
TalkingClock t = new TalkingClock();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(int interval, boolean beep){
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("At the tone, the time is " + new Date());
if(beep)Toolkit.getDefaultToolkit().beep();
}
};
Timer t = new Timer(interval,listener);
t.start();
}
}
//反编译
public class TalkingClock {
public static void main(final String[] args) {
final AICTest t = new AICTest();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(final int interval, final boolean beep) {
class TalkingClock$1 implements ActionListener {
private final /* synthetic */ boolean val$beep;
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("At the tone, the time is " + new Date());
if (this.val$beep) {
Toolkit.getDefaultToolkit().beep();
}
}
}
final Timer t = new Timer(interval, listener);
t.start();
}
}
//反射
public class com.TalkingClock {
public com.TalkingClock();
public static void main(java.lang.String[]);
public void start(int, boolean);
}
class com.TalkingClock$1 implements java.awt.event.ActionListener {
final com.TalkingClock this$0;
private final boolean val$beep;
com.TalkingClock$1(com.TalkingClock, boolean);
public void actionPerformed(java.awt.event.ActionEvent);
}
//Lambda表达式匿名内部类
public class TalkingClock {
public static void main(String[] args) {
// TODO Auto-generated method stub
TalkingClock t = new TalkingClock();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(int interval, boolean beep){
Timer t = new Timer(interval, event->
{
System.out.println("At the tone, the time is " + new Date());
if(beep)Toolkit.getDefaultToolkit().beep();
});
t.start();
}
}
//反编译
public class TalkingClock
{
public static void main(final String[] args) {
final TalkingClock t = new TalkingClock();
t.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
public void start(final int interval, final boolean beep) {
final Timer t = new Timer(interval, event -> {
System.out.println("At the tone, the time is " + new Date());
if (beep) {
Toolkit.getDefaultToolkit().beep();
}
return;
});
t.start();
}
}
//反射
public class com.TalkingClock {
public com.niming.TalkingClock();
public static void main(java.lang.String[]);
public void start(int, boolean);
private static void lambda$0(boolean, java.awt.event.ActionEvent);
}
静态内部类
//静态内部类代码
public class StaticInnerClassTest {
public static void main(String[] args) {
double[] d = new double[20];
for (int i = 0; i < d.length; i++)
d[i] = 100 * Math.random();
ArrayAlg.Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
}
class ArrayAlg {
public static class Pair {
private double first;
private double second;
public Pair(double f, double s) {
first = f;
second = s;
}
public double getFirst() {
return first;
}
public double getSecond() {
return second;
}
}
public static Pair minmax(double[] values) {
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (double v : values) {
if (min > v)
min = v;
if (max < v)
max = v;
}
return new Pair(min, max);
}
}
//反编译
public class Test {
public static void main(final String[] args) {
final double[] d = new double[20];
for (int i = 0; i < d.length; ++i) {
d[i] = 100.0 * Math.random();
}
final ArrayAlg.Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
}
class ArrayAlg {
public static Pair minmax(final double[] values) {
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (final double v : values) {
if (min > v) {
min = v;
}
if (max < v) {
max = v;
}
}
return new Pair(min, max);
}
public static class Pair {
private double first;
private double second;
public Pair(final double f, final double s) {
this.first = f;
this.second = s;
}
public double getFirst() {
return this.first;
}
public double getSecond() {
return this.second;
}
}
}
//反射
public class com.Test {
public com.Test();
public static void main(java.lang.String[]);
}
class com.ArrayAlg {
com.ArrayAlg();
public static com.ArrayAlg$Pair minmax(double[]);
}
public class com.ArrayAlg$Pair {
private double first;
private double second;
public com.ArrayAlg$Pair(double, double);
public double getFirst();
public double getSecond();
}