一、内部类
将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)
访问特点:
非static内部类可以直接访问外部类中的成员,包括私有成员。
原因:
因为内部类中持有了一个外部类的引用,
引用的格式为: 外部类名.this
非静态内部类不能定义static成员
二、非静态内部类的分类
1、 内部类定义在外部类的成员位置上(方法外部):
1>可以被private 、 static 成员修饰符修饰。
被Private修饰: 将内部类在外部类中进行封装。
被static修饰的内部类只能访问外部类中的静态成员,出现了访问权限、
2>在外部其它类中创建非static内部类的实例变量:必须要先建立外部类的实例对象,然后通过外部类的实例对象去创建内部类的实例对象。
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // new Outer().new Inner( )
3>在外部其他类中,直接访问static内部类的非静态成员:
用内部类对象去调用非静态成员: new Outer.Inner( ).function( );
4>在外部其他类中,直接访问static内部类的静态成员:
Outer.Inner.function();
内部类中变量的访问代码示例:
外部类要访问内部类中的成员必须要先建立外部类的实例对象,然后通过外部类的实例对象去创建内部类的实例对象。
访问格式:当内部类定义在外部类的成员上,且非私有,可以在外部其他类中直接建立内部类对象
Outer.Inner in = new Outer().new Inner();
in.function( );
class Outer {
private int x = 3;
class Inner// 内部类
{
//int x = 4;
void function() {
//int x = 6;
//System.out.println("innner的局部变量 x =6 的访问格式:x = " + x);
//System.out.println("innner的成员变量 x =4 的访问格式:this.x = " + this.x);
// 内部类能直接访问外部类成员的原因是省略了Outer.this.
System.out.println("访问Outer的成员变量 x = 3 省略了Outer.this 的结果: " + x);
System.out.println("访问Outer的成员变量 x = 3 使用全称格式:Outer.this.x 的结果: "
+ Outer.this.x);
}
}
void method() {
Inner in = new Inner();
in.function();
}
}
class InnerClassDemo {
public static void main(String[] args) {
Outer out = new Outer();
out.method();
// 直接访问内部类中的成员。
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
被static修饰的方法,内部类,访问示例:
class Outer2{
private static int x = 3;
//静态内部类,方法也是静态
static class Inner{
static void function()//当内部类中定义了静态成员,内部类也必须是静态
{
System.out.println("innner :"+x);
}
}
//静态内部类,方法不是静态
static class Inner2{
void show()
{
System.out.println("inner2 show");
}
}
public static void method(){
//Inner.function(); //访问静态类的静态方法
new Inner2().show();//当外部类中的静态方法访问内部类时,内部类也必须是静态的
//因为show非静态,需要对象访问,所以new Inner2()
//new Outer2().new Inner2().show();//当Inner2不是static时的调用方式
}
}
class InnerClassDemo2{
public static void main(String[] args){
Outer2.method();
//在外部其他类中,直接访问static内部类的非静态成员:用内部类对象去调用非静态成员
new Outer2.Inner2().show();
//在外部其他类中,直接访问static内部类的静态成员:
Outer2.Inner.function( );
}
}
2、 内部类定义局部位置上(方法内部)
1 、该内部类也可以直接访问外部类中的成员,因为还持有外部类中的引用
2、不能有访问类型修饰符,就好像方法中定义的局部变量一样。但该内部类前面可以使用abstract和final修饰符。
3、这种类对其它类是不可见的,其它类无法引用这种内部类,但该内部类所创建的实例对象可以传递给其它类访问就好像方法中定义的局部变量一样,
2、这种内部类可以访问所在方法体中的局部变量,但是必须是被final关键字修饰的。
内部类定义在局部代码演示:
class Outer{
int x = 3;
void method(final int a){ //只能访问被final修饰的局部变量。
final int y = 4; //内部类定义在局部时,只能访问被final修饰的局部变量。
//局部内部类不能被private,static修饰,因为它们只修饰成员
//所以局部内部类也不能定义静态成员
class Inner{
void function(){
System.out.println(a);
System.out.println(x);
System.out.println(y);
}
}
new Inner().function();
}
}
class InnerClassDemo3{
public static void main(String[] args) {
Outer out = new Outer();
out.method(7);
out.method(8);
}
}
三、静态内部类Static Nested Class分类
1>在成员位置上(方法外部定义的内部类),在前面可以加上static关键字,从而成为Static Nested Class
它不再具有内部类的特性,所以,从狭义上讲,它不是内部类。
Static Nested Class与普通类在运行时的行为和功能上没有什么区别,只是在编程引用时的语法上有一些差别,它可以定义成public、protected、默认的、
private等多种类型,而普通类只能定义成public和默认的这两种类型。在外面引用Static Nested Class类的名称为“外部类名.内部类名”。在外面不需要创
建外部类的实例对象,就可以直接创建Static Nested Class。
例如,假设Inner是定义在Outer类中的Static Nested Class,那么可以使用如下语句创建Inner类:
Outer.Inner inner = new Outer.Inner();
2>在静态方法中定义的内部类也是Static Nested Class,这时候不能在类前面加static关键字
静态方法中的Static Nested Class与普通方法中的内部类的应用方式很相似,它除了可以直接访问外部类中的static的成员变量,还可以访问静态方法中
的局部变量,但是,该局部变量前必须加final修饰符。
注意:
当内部类中定义了静态成员,该内部类也必须是静态的,即非静态内部类不能定义静态成员。
当外部类中的静态方法访问内部类时,该内部类也必须是静态的,静态只能访问静态
内部类定义原则:
当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。
四、匿名内部类
1 就是内部类的简化写法。
2、前提:
内部类可以继承或实现一个外部类或者实现接口
3、格式:
new 外部类名或者接口名( ){
覆盖类或者接口中的代码(也可以自定义内容)
};
4、就是建立一个带有内容的外部类或者接口的子类匿名对象
5、匿名内部类中定义的方法最好不要超过3个
匿名内部类书写示例代码:
class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
}
}
abstract class AbsDemo{
abstract void show();
}
class Outer{
int x = 3;
/*
class Inner extends AbsDemo
{
int num = 90;
void show()
{
System.out.println("show :"+num);
}
void abc()
{
System.out.println("hehe");
}
}
*/
public void function(){
//AbsDemo a = new Inner();
// Inner in = new Inner();
// in.show();
// in.abc();
//匿名内部类中,Inner就没有了,new父类,然后实现抽象方法。
AbsDemo d = new AbsDemo(){//相当于:AbsDemo a = new Inner(),父类引用指向子类对象,多态
int num = 9;
//实现抽象方法
void show(){
System.out.println("num==="+num);
}
void abc(){
System.out.println("haha");
}
};
d.show();
//d.abc();//编译失败,因为父类中没有定义
}
}
五、匿名内部类练习
//练习一:补足代码。通过匿名内部类。
interface Inter{
void method();
}
class Test{
//补足代码。通过匿名内部类。
/*
static class Inner implements Inter{
public void method(){
System.out.println("method run");
}
}
static function (){
return new Inner();
}
*/
static Inter function(){
return new Inter(){
public void method(){
System.out.println("method run");
}
};
}
}
class InnerClassTest1 {
public static void main(String[] args) {
//Test.function():Test类中有一个静态的方法function。
//.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
//因为只有是Inter类型的对象,才可以调用method方法。
Test.function().method();
// 相当于:
// Inter in = Test.function();
// in.method();
}
//练习二:匿名内部类作为函数参数
//当使用的方法接收的参数是一个接口时,该接口中的方法较少,可以定义一个匿名内部类传进去
public static void show(Inter in){
in.method();
}
}
show(new Inter(){
public void method(){
System.out.println("method show run");
}
});
//练习三:没有“一般”(因为有上帝:Object)父类,也没有接口的情况下,创建匿名内部类
class InnerClassTest3{
public static void main(String[] args){
//Object obj = new Object……不可以,Object中没有function
new Object(){
public void function(){
}
}.function();
}
}