phoebus0501

在java语言中,有一种类叫做内部类(inner class),也称为嵌入类(nested class),它是定义在其他类的内部。内部类作为其外部类的一个成员,与其他成员一样,可以直接访问其外部类的数据和方法。只不过相比较外部类只有public和默认的修饰符不同,内部类作为一个成员,可以被任意修饰符修饰。编译器在编译时,内部类的名称为outerclass$innerclass.class 。

1、内部类访问数据变量

当在某些时候,内部类中定义的变量与外部类中变量名称相同时,如何确保正确地访问每一个变量呢?

1.1在main中直接从外部类调用内部类的方法

class outer

{

private int index = 10;

class inner

{

private int index = 20;

void print()

{

int index = 30;

system.out.println(this); // the object created from the inner

system.out.println(outer.this); // the object created from the outer

system.out.println(index); // output is 30

system.out.println(this.index); // output is 20

system.out.println(outer.this.index); // output is 10

}

}

void print()

{

inner inner = new inner();//得到内部类的引用

inner.print();

}

}

class test

{

public static void main(string[] args)

{

outer outer = new outer();

outer.print();

}

}

在这里内部类inner中关键字this指向内部类inner的对象,如果要想指向外部类的对象,必须在this指针前加上外部类名称,表示this是指向外部类构造的碎屑,如outer.this 。

1.2在main中显式返回内部类引用

class outer

{

private int index = 10;

class inner

{

private int index = 20;

void print()

{

int index = 30;

system.out.println(index);

system.out.println(this.index);

system.out.println(outer.this.index);

}

}

inner getinner()

{

return new inner();//返回一个内部类的引用

}

}

class test

{

public static void main(string[] args)

{

outer outer = new outer();

outer.inner inner = outer.getinner();

inner.print();

}

}

inner是outer的内部类,所以在类test中必须用属性引用符来标识出内部类。

1.3当main方法在outer类内部

class outer

{

private int index = 10;

class inner

{

private int index = 20;

void print()

{

int index = 30;

system.out.println(index);

system.out.println(this.index);

system.out.println(outer.this.index);

}

}

inner getinner()

{

return new inner();//返回一个内部类的引用

}

public static void main(string[] args)

{

outer outer = new outer();

inner inner = outer.getinner(); // 注意此处变化

inner.print();

}

}

因为main方法在outer内部,故可以直接引用,不需要属性引用符。

1.4在main方法中直接产生内部类对象

class test

{

public static void main(string[] args)

{

outer outer = new outer();

outer.inner inner = outer.new inner(); // 注意此处变化

inner.print();

}

}

在利用new构造方法构造一个外部类对象时,并没有连带着构造一个内部类对象,故需要访问内部类方法时,必须使用new操作符为这个外部类对象再构造一个内部类对象。

2、局部内部类

在方法中定义的内部类是局部内部类,它只能访问方法中的final类型的局部变量,因为用final定义的局部变量相当于是一个常量,延长了其生命周期,使得方法在消亡时,其内部类仍可以访问该变量。另外,它同样也可以引用定义在外部类的变量和方法。而且方法体中的局部内部类不允许有访问修饰符。

class outer

{

int num=10;

public void print(final int aargs)

{

class inner

{

int num=20;

public inner()

{

system.out.println("this is inner.");//此句可看出它与匿名内部类用法的不同。

}

public void print()

{

int num=30;

system.out.println(this); // the object created from the local inner

system.out.println(num);

system.out.println(this.num);

system.out.println(outer.this.num);

system.out.println(aargs);

}

}

inner inner=new inner();//此句必须放在定义类inner的后面

inner.print();

}

public static void main(string[] args)

{

outer outer=new outer();

outer.print(40);

}

}

对于局部类的命名,不管是在一个方法中定义多个类还是在几个方法中分别定义类,其编译后命名是:outerclass$1innerclass.class

3、匿名内部类

匿名内部类作为一种特殊的内部类,除了具有普通内部类的特点,还有自己的一些独有特性:

匿名内部类必须扩展一个基类或实现一个接口,但是不能有显式的extends和implements子句;

匿名内部类必须实现父类以及接口中的所有抽象方法;

匿名内部类总是使用父类的无参构造方法来创建实例。如果是实现了一个接口,则其构造方法是object();

匿名内部类编译后的命名为:outerclass$n.class,其中n是一个从1开始的整数,如果在一个类中定义了多个匿名内部类,则按照他们的出现顺序从1开始排号。

abstract class a

{

abstract public void sayhello();

}

class outer

{

public static void main(string[] args)

{

new outer().callinner(new a()

{

public void sayhello()

{

system.out.println(this); // the object created from the anonymous inner

system.out.println("hello!");

}

});

}

public void callinner(a a)

{

a.sayhello();

}

}

4、静态内部类

和非静态内部类相比,区别就在于静态内部类没有了指向外部类的引用。除此之外,在任何非静态内部类中,都不能有静态数据,静态方法或者又一个静态内部类(内部类的嵌套可以不止一层)。不过静态内部类中却可以拥有这一切。这也算是两者的第二个区别吧。一个静态的内部类,才可以声明一个static成员,静态内部类可以访问外围类的静态方法、成员(包括private static的成员)。静态内部类实例化的时候不必先实例化外围类,可以直接实例化内部类。而对于非静态内部类则必须先实例化其外部类,才能再实例化本身。

5.内部类的继承

当一个类继承自一个内部类时,缺省的构造器不可用。必须使用如下语法:

class withinner

{

class inner

{

public void sayhello()

{

system.out.println("hello.");

}

}

}

public class test extends withinner.inner

{

test(withinner wi)

{

wi.super();

}

public static void main(string[] args)

{

withinner wi=new withinner();

test test=new test(wi);

test.sayhello();

}

}

因为每一个内部类都有一个指向外部类的引用,在继承一个内部类,必须先创建一个外部类,通过这个外部类引用来调用其内部类的构造方法。如果继承的内部类是一个静态内部类,则就不需要这样,直接super()调用即可;

6、内部类的2种特殊用法

一个类从另一个类派生出来,又要实现一个接口。但在接口中定义的方法与父类中定义的方法的意义不同,则可以利用内部类来解决这个问题。

interface machine

{

void run();

}

class person

{

void run()

{

system.out.println("run");

}

}

class robot extends person

{

private class machineheart implements machine

{

public void run()

{

system.out.println("heart run");

}

}

machine getmachine()

{

return new machineheart();

}

}

class test

{

public static void main(string[] args)

{

robot robot = new robot();

machine m = robot.getmachine();

m.run();

robot.run();

}

}

在robot类内部使用内部类machineheart来实现接口machine的run方法。同时robot类又继承了父类person的run方法。如果不使用内部类machineheart而使robot直接实现接口machine,则该如何调用父类的run方法?

利用内部类可解决c++中多重继承所解决的问题

class a

{

void fn1()

{

system.out.println("it' s fn1.");

}

}

abstract class b

{

abstract void fn2();

}

class c extends a

{

b getb()

{

return new b()

{

public void fn2()

{

system.out.println("it' s fn2.");

}

};

}

}

class test

{

public static void main(string[] args)

{

c c = new c();

c.fn1();

c.getb().fn2();

}

}

类c既要继承类a又要继承类b,则可将类b的定义放入类c内部,使之成为内部类。

一般情况下 当我们需要在某一情形下实现一个接口,而在另一情形下又不需要实现这个接口时,我们可以使用内部类来解决这一问题。让内部类来实现这个接口。另外一个很好的理由是java内部类加上接口可以有效地实现多重继承。

参考资料:http://hi.baidu.com/kiddoneal/blog/item/3b6a68864fd75a20c75cc348.html

绿色通道:好文要顶关注我收藏该文与我联系


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值