---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
内部类
内部类可以继承外部类或实现接口内部类的访问规则:
1.内部类可以直接访问外部类的成员,包括私有
之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式:外部类名.this
2.外部类要访问内部类,必须建立内部类对象
注:内部类可以被成员修饰符所修饰,比如private、static修饰。内部类是外部类成员
当内部类被static修饰后,只能直接访问外部类中的static成员,有了访问局限性
在外部其他类中,如何直接访问static内部类的非静态成员?
new Outer.Inner().function();
在外部其他类中,如何直接访问static内部类的静态成员?
Outer.Inner.function();
注:当内部类中定义了静态成员,该内部类必须是static修饰的
当外部类中的静态方法访问内部类时,内部类也必须是static的
为什么方法中的内部类只能访问有final修饰的局部变量:
在Java中,方法的局部变量位于栈上,对象位于堆上。因为局部变量的范围被限制在该方法内,当一个方法结束时,栈结构被删除,该变量消失。但是,定义在这个类中的内部类对象仍然存活在堆上,所以内部类对象不能使用局部变量。除非这些局部变量被标识为最终的。final int i=8;就可以
class Outer
{
private int x=3;
class Inner
{
int x=4;
void function()
{
int x=5;
System.out.println(x); // 5
System.out.println(this.x); // 4
System.out.println(Outer.this.x); // 3 外部类的引用
}
}
void method()
{
new Inner().function(); //外部类访问内部类要建立内部类对象之后才可以访问
}
static class InnerStatic
{
void show()
{
System.out.println("外部类中的静态方法访问内部类中的成员,内部类必须被static修饰");
}
}
public static void outMethod()
{
new InnerStatic().show();
}
}
class InnerDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method(); //通过外部访问内部类方法
//直接访问内部类成员 (当内部类Inner未被private修饰的情况下)
Outer.Inner in = new Outer().new Inner(); //格式:外部类名.内部类名 变量名 = 外部类对象.内部类对象
in.function();
Outer.outMethod();
}
}
什么时候使用内部类?
当描述事物时,事物的内部还有事物,该事物使用内部类来描述,因为这类事物要直接使用外部事物的内容
示例:
class Body //人体
{
private class Heart //心脏
{
}
public void show()
{
new Heart();
}
}
内部类还可以放在局部中,但是局部中的内部类不能被成员修饰符修饰,并且该内部类中的方法也不能被成员修饰符修饰。
调用局部内部类中的方法示例:
class Outer
{
int x = 3;
void method(final int a)
{
//a++; 这样写会出错,原因是a被final修饰,只能被赋值一次。
final int y = 4; //内部类访问其所在的局部变量时,只能访问被final修饰的局部变量
class Inner
{
void function()
{
System.out.println(x);
System.out.println(y);
System.out.println(a);
}
}
new Inner().function(); //这条语句要放在Inner类下面,因为method()方法被调用时才会加载。若放在上面内存会找不到Inner类导致创建不了对象,从而编译失败
}
}
class InnerDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method(5); //调用时,进栈;调用完,出栈。出栈之后a便可以重新赋值
out.method(6);
}
}
匿名内部类:
1.匿名内部类其实就是内部类的简写格式
2.定义匿名内部类的前提:内部类必须是继承一个类或实现接口
3.匿名内部类的格式:new 父类或者接口(){定义子类内容}
4.其实匿名内部类就是一个匿名子对象,可以理解为带内容的对象
5.匿名内部类中定义的方法最好不要超过3个。
示例:
abstract AbsDemo
{
abstract void show();
}
class Outer
{
int x = 3;
/*
class Inner extends AbsDemo
{
void show()
{
System.out.println("x="+x);
}
}
*/
void function()
{
//new Inner().show();
//下面这段代码代表上面被注释的代码,匿名内部类
new AbsDemo()
{
void show()
{
System.out.println("x="+x);
}
void abc()
{
System.out.println("haha");
}
}.show();
}
}
class InnerDemo
{
public static void main(String[] args)
{
new Outer().function();
}
}
匿名内部类应用:
可能会遇到的面试题:
没有接口,也没有父类,是否能创建内部类实现function()方法?
实现方法:
class InnerClassDemo
{
public static void main(String[] args)
{
new Object()
{
void function()
{
System.out.println("Object是根类");
}
}.function();
}
}
应用二:根据主函数执行语句补充未知函数
interface Inter
{
void method();
}
class Test
{
//补充函数,使用匿名内部类
public static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println();
}
};
}
}
class InnerClassTest
{
public static void main(String[] args)
{
Test.function().method();
//Test.function(); 说明function()方法是静态的
//.method():function这个方法运算结果返回的是一个对象,而且是一个Inter类型的对象
//因为只有Inter类型才可以调用method()方法
//可拆分为 Inter in = Test.function(); in.method();
}
}
---------------------- ASP.Net+Android+IOS开发、 .Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net