------------android培训、java培训、期待与您交流! --------------
大纲:
一、内部类概念
二、成员内部类
三、静态内部类
四、局部内部类
五、匿名内部类
一、内部类概念:
将一个类定义在另一个类的里面,那么这个类就叫做内部类,或者嵌套类。
内部类可以直接访问外部类中的成员,包括私有成员。而外部类要访问内部类中的成员必须要建立内部类对象。
四种内部类:成员内部类、局部内部类、静态内部类、匿名内部类。
使用内部类的原因是,当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事物在使用外部事物的内容。直接定义在外部类里面会很容易的使用外部类信息。而且
有的情况从现实世界中考虑,用内部类更符合逻辑。比如把人的身体定义为一个外部类,人的心脏
就因该定义在里面。
二、成员内部类:
内部类定义在外部类里面,相当于外部类的一个成员,可以被成员修饰符所修饰。
示例代码1(定义成员内部类)
class Outer {
private int x = 3;
class Inner// 内部类
{
// int x = 4;
void function() {
// int x = 6;
System.out.println("innner :" + 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();
}
}
1、在function方法内部要想访问内部成员变量x用this.x,要想访问外部成员变量x要用Outer.this.x
2、访问成员内部类成员有两种方式:
a:可以在外部类方法中创建该内部类的对象,然后调用内部类方法;
b:如果内部类是非私有的,可以在外部其他类中直接建立内部类对象。
格式:
外部类名.内部类名 变量名 = 外部类对象.内部类对象;
Outer.Inner in = new Outer().new Inner();
私有成员内部类:
当内部类在成员位置上,就可以被成员修饰符所修饰。比如,private:将内部类在外部类中进行封装。
并提供返回内部类对象的方法。那么外部其他类只能通过外部类提供的方法访问内部类。程序会更加
健壮。如下列代码(模拟身体和心脏):
class Body
{
private class XinZang
{
}
public void show()
{
new XinZang().
}
}
心脏是内部事物不能直接被外部访问,只能通过show方法访问。
三、静态内部类:
静态内部类就具备static的特性。
当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。
a:在外部其他类中,直接访问static内部类的非静态成员格式:
new Outer.Inner().function();
b:在外部其他类中,直接访问static内部类的静态成员格式:
Outer.Inner.function();
注意:当内部类中定义了静态成员,该内部类必须是static的。
当外部类中的静态方法访问内部类时,内部类也必须是static的。
示例代码2(静态内部类)
class Outer {
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();
}
}
四、局部内部类:
内部类定义在局部时,
1,不可以被成员修饰符修饰
2,可以直接访问外部类中的成员,因为还持有外部类中的引用。
但是不可以访问它所在的局部中的变量。只能访问被final修饰的局部变量。
class Outer
{
int x = 3;
void method(final int a)
{
final int y = 4;
class Inner
{
void function()
{
System.out.println(y);
// System.out.println(x);
}
}
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个。
abstract class AbsDemo {
abstract void show();
}
class Outer {
int x = 3;
public void function() {
// AbsDemo a = new Inner();
// Inner in = new Inner();
// in.show();
// in.abc();
AbsDemo d = new AbsDemo() {
int num = 9;
void show() {
System.out.println("num===" + num);
}
void abc() {
System.out.println("haha");
}
};
d.show();
// d.abc();//编译失败;
}
}
class InnerClassDemo4 {
public static void main(String[] args) {
new Outer().function();
}
}
匿名内部类经典示例:
interface Inter {
void method();
}
class Test {
// 补足代码。通过匿名内部类。
static Inter function() {
return new Inter() {
public void method() {
System.out.println("method run");
}
};
}
}
class InnerClassTest {
public static void main(String[] args) {
// Test.function():Test类中有一个静态的方法function。
// .method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
// 因为只有是Inter类型的对象,才可以调用method方法。
Test.function().method();
// Inter in = Test.function();
// in.method();
show(new Inter() {
public void method() {
System.out.println("method show run");
}
});
}
public static void show(Inter in) {
in.method();
}
}
class InnerTest {
public static void main(String[] args) {
new Object() {
public void function() {
}
}.function();
}
}
------------android培训、java培训、期待与您交流! --------------