内部类访问规则
一、内部类的访问规则:
1.内部类可以直接访问外部类中的成员,包括私有
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用————格式: 外部类名.this
2.外部类要访问内部类,必须建立内部类对象
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用————格式: 外部类名.this
2.外部类要访问内部类,必须建立内部类对象
访问格式:
1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,可以直接建立内部类对象。
格式
外部类明.内部类名. 变量名=外部类对象.内部类对象;
例如例中的:Outer.Inner in=new Outer().new Inner();
2.当内部类定义在成员位置上,就可以被成员修饰符所修饰。
比如,private :将内部类在外部类中进行封装。
static : 内部类就具备了静态的特性
二、代码
class Outer
{
private int x=3;
class Inner //内部类
{
int x=4;
void function()
{
int x=6;
System.out.println("inner:"+x);//6
System.out.println(this.x);//4
System.out.println(Outer.this.x);//3
}
}
void method()
{
Inner in=new Inner();
in.function();
System.out.println(x);
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Outer out=new Outer();
out.method();
//直接访问内部类中的成员
Outer.Inner in=new Outer().new Inner();
in.function();
}
}
静态内部类
一、概述
当内部类被static修饰后,只能直接访问外部类中的static成员,即,出现了访问局限
问题1:那么在外部其他类中,如何直接访问static内部类中的非静态成员呢?
new Outer.Inner1().function();
问题2.那么在外部其他类中,那么如何直接访问static内部类中的静态成员呢?
Outer.Inner1.function();
问题1:那么在外部其他类中,如何直接访问static内部类中的非静态成员呢?
new Outer.Inner1().function();
问题2.那么在外部其他类中,那么如何直接访问static内部类中的静态成员呢?
Outer.Inner1.function();
注意:
1.当内部类中定义了静态成员,该内部类必须是static的。
2.当外部类中的静态方法访问内部类时,内部类也必须是static的。
二、代码
class Outer
{
private static int x=3;
static class Inner1//会编译失败,因为静态内部类无法访问非静态成员。
{
void function()
{
System.out.println("inner1:"+x);
}
static void function1()
{
System.out.println("inner1: static"+x);
}
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
new Outer.Inner1().function();
Outer.Inner1.function1();
}
}
内部类定义原则
一、概述
当描述事物时,事物的内部还有事物,这事物由内部类来藐视。
因为:内部事物在使用外部事物的内容。
例子:人——心脏等器官,心脏就可以定义为“人”这个类的内部类。
二、代码
class Body
{
private class XinZang
{
}
}
内部类定义在局部
一、概述
内部类定义在局部时
1.不可以被成员修饰符修饰。
2.可以直接访问外部类中的成员,因为还持有外部类中的引用。
但是不可以访问他所在的局部中的变量,只能访问被final所修饰的局部变量。
1.不可以被成员修饰符修饰。
2.可以直接访问外部类中的成员,因为还持有外部类中的引用。
但是不可以访问他所在的局部中的变量,只能访问被final所修饰的局部变量。
二、代码
class Outer
{
int x=3;
void method(final int a)
{
final int y=4//要被内部类访问的局部变量,要加上final
//new Inner().function();会报错
class Inner
{
void function()
{
System.out.println(y);
System.out.println(a);
}
}
new Inner().function();
}
}
class InnerClassDemo3
{
public static void main(String[] args)
{
Outer out=new Outer();
out.method(7);
out.method(8);
}
}
匿名内部类
一、概述
匿名内部类:
1.匿名内部类其实就是一个内部类的简写格式。
2。定义匿名内部类的前提
内部类必须是集成一个类或者实现接口
3.匿名内部类的格式:
new 父类或者接口()
{
定义子类的内容;
}
4.匿名内部类,就是一个匿名子类对象。
5.匿名内部类中定义的方法最好不要超过3个,因为方法过多会降低程序阅读性。
1.匿名内部类其实就是一个内部类的简写格式。
2。定义匿名内部类的前提
内部类必须是集成一个类或者实现接口
3.匿名内部类的格式:
new 父类或者接口()
{
定义子类的内容;
}
4.匿名内部类,就是一个匿名子类对象。
5.匿名内部类中定义的方法最好不要超过3个,因为方法过多会降低程序阅读性。
二、代码
①练习一
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x=3;
/*①*/
class Inner extends AbsDemo
{
void show()
{
System.out.println("show:"+x);
}
}
public void function()
{
new Inner().show();
}
/*②*/
public void function1()
{
AbsDemo a=new AbsDemo()
{
int num=9;
public void show()
{
System.out.println("num="+num);
}
};
new AbsDemo()
{
int num=9;
public void show()
{
System.out.println("num="+num);
}
}.show();
a.show();
//a.abc();因为是多态,所以不可以调用子类特有方法。
}
}
class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
new Outer().function1();
}
}
②练习二
interface Inter
{
void method();
}
class Test
{
static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println("haha");
}
};
}
}
class InnerClassDemo5
{
public static void main(String[] args)
{
Test.function().method();
}
}
③练习三
class InnerClassDemo6
{
public static void main(String[] args)
{
new Object()
{
public void method()
{
System.out.println("heihei");
}
}.method();
}
}