内部类即在一个类的内部定义了其他类的情况.
范例1:
class Outer{ //外部类
private String msg = "hello world" ;//外部的私有操作
class Inner{ //内部类
public void print(){
System.out.println(msg) ;
}
}
public void fun(){
Inner in = new Inner() ;
in.print() ;
}
}
public class TestDemo{
public static void main(String args[]){
Outer out = new Outer() ;
out.fun() ;
}
}
以上程序的执行过程:实例化对象 out→调用方法→调用方法实例化内部的对象→调用内部的方法
运行结果:
由以上程序可以看出,内部类的最大缺点是破坏了程序结构,使得本应该只含有属性和方法的类中又多出了一个类,但是,内部类的最大优点就是:可以方便地访问外部类的私有操作,当然也可以由外部类方便地访问内部类的私有操作,不再需要setter和getter.
范例2:
class Outer{
class Inner{
private String msg = "你好" ;
}
public void fun(){
Inner in = new Inner() ;
System.out.println(in.msg) ;
}
}
public class TestDemo{
public static void main(String args[]){
Outer out = new Outer() ;
out.fun() ;
}
}
运行结果:
标准:当访问类中的属性时前面应该加上"this.",但是对于以上的程序,内部类访问外部属性的时候,使用"this."只能表示内部类的当前对象,所以,如果想要表示出外部类的当前对象,应该使用"外部类.this"来表示.
范例3:
class Outer{
private String msg = "hello" ;
class Inner{
public void print(){
System.out.println(Outer.this.msg) ;
}
}
public void fun(){
Inner in = new Inner() ;
in.print() ;
}
}
public class TestDemo{
public static void main(String args[]){
Outer out = new Outer() ;
out.fun() ;
}
}
运行结果:
以上程序是通过外部类的方法操作了内部类.那么如何在其他不同的类中访问内部类呢?
首先我们查看一下以上程序的*.class文件:
可以看到,内部类的*.class文件的名称为Outer$Inner.class
$ 是在文件中的表示,换到程序中" $ "表示" . ",所以内部类的类名称为Outer.Inner,由此可以得出在外部实例化内部类对象的操作格式:
外部类.内部类 内部类对象 = new 外部类().new 内部类() ;
范例4:
class Outer{
private String msg = "hello" ;
class Inner{
public void print(){
System.out.println(Outer.this.msg) ;
}
}
}
public class TestDemo{
public static void main(String args[]){
Outer.Inner in = new Outer().new Inner() ;
in.print() ;
}
}
运行结果:
如果,内部类不想被外部使用,可以使用private将其定义为私有内部类(仅仅内部类外层的那个外部类可以使用).
错误例:
class Outer{
private String msg = "hello" ;
private class Inner{
public void print(){
System.out.println(Outer.this.msg) ;
}
}
}
public class TestDemo{
public static void main(String args[]){
Outer.Inner in = new Outer().new Inner() ;
in.print() ;
}
}
运行结果:
定义内部类时,也可以使用static定义,但是使用了static定义的内部类表示"外部类",但是,它只能访问外部类中的static类型的操作.
class Outer{
private static String msg = "hello" ;//static类型!!
static class Inner{
public void print(){
System.out.println(msg) ;//没有Outer,没有this!!
}
}
}
public class TestDemo{
public static void main(String args[]){
Outer.Inner in = new Outer.Inner() ;
in.print() ;
}
}
运行结果:
理论上,内部类可以定义在类的任何位置上,以下是在方法中定义内部类.
范例7:
class Outer{
private String msg = "hello" ;
public void fun(){
class Inner{//方法中定义的内部类
public void print(){
System.out.println(Outer.this.msg) ;
}
}
Inner in = new Inner() ;
in.print() ;
}
}
public class TestDemo{
public static void main(String args[]){
new Outer().fun() ;
}
}
当定义在方法中的内部类要访问方法的参数或者是方法中定义的变量时,这些参数或变量前一定要增加一个final关键字,否则无法调用.
范例8:
class Outer{
private String msg = "hello" ;
public void fun(final int x){
class Inner{//方法中定义的内部类
public void print(){
final String info = "world" ;
System.out.println(Outer.this.msg) ;
System.out.println(x) ;
System.out.println(info) ;
}
}
Inner in = new Inner() ;
in.print() ;
}
}
public class TestDemo{
public static void main(String args[]){
new Outer().fun(30) ;
}
}
运行结果: