内部类
将一个类定义在另一个类的里面,对立面那个类就成为内部类(内置类,嵌套类)
定义:当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。
举例子心脏
内部类的访问规则
1内部类可以直接访问外部类中的成员,包括私有。
之所以可以直接访问外部类的成员 ,因为内部类中持有一个外部类的引用。
格式: 外部类名.this。
2外部类要访问内部类,必须建立内部类对象。
class Outer
{
int x=3;
class Inner //内部类
{
void function()
{
System.out.println("inner:");
}
}
void method()
{
Inner in =new Inner();
in.function();
}
}
class Test
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method();
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
内部类访问外部类 格式: 外部类名.this
class Outer
{
int x=3; //System.out.println("inner:"+Outer.this.x);
class Inner //内部类
{
int x=4; //System.out.println("inner:"+this.x);
void function()
{
int x=5 ; //System.out.println("inner:"+x);
System.out.println("inner:"+x);
}
}
内部类在成员位置上
1内部类定义 在 外部类 成员位置上,且非私有,
外部其他类中,可以直接建立内部类对象
外部类名.内部类名 变量名=外部类对象.内部类对象
Outer是外部类,Inner是非静态内部类。
Outer.Inner in = new Outer().new Inner();
2内部类在成员位置,可以被成员修饰符修饰
private: 将内部类在外部类中进行封装;
static : 内部类就具备了static的特性。
static class Inner{}
当内部类被static修饰后,只能直接访问外部类中的被static修饰的成员。
在外部其他类中,直接访问静态内部类的非静态成员。
Outer是外部类,Inner是静态内部类 。
Outer.Inner in = new Outer.Inner.function();
3 当内部类中成员定义了static,该类也必须被static修饰。
4当外部类中的static成员访问内部类中的成员时,内部类必须也是static。
class Outer
{
int x=3;
static class Inner
{
void function()
{
System.out.println("inner:"+x);
}
}
}
class Test
{
public static void main(String[] args)
{
new Outer.Inner().function();
}
}
在外部其他类中,直接访问静态内部类的静态成员。
class Outer
{
int x=3;
static class Inner
{
static void function()
{
System.out.println("inner:"+x);
}
}
}
class Test
{
public static void main(String[] args)
{
Outer.Inner.function();
}
}
当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。
class Body
{
private class XinZang
{
}
public void show()
{
new XinZang()
}
}
内部类定义在局部时
1.不可以被成员修饰符修饰。 static public private
2.可以直接访问外部类成员,因为还持有外部类中的引用。
**但是不可以直接访问所在的局部中的变量,只能访问被final修饰的局部变量。
内部类的调用 不可以被成员修饰符修饰。 static public private
class Outer
{
int x=3;
void method()
{
class Inner
{
void function()
{
System.out.println(Outer.this.x);
}
}
new Inner().function(); //只有先加载完Inner,才能new对象
}
}
class Test
{
public static void main(String[] args)
{
new Outer().method();
}
}
不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。
</pre><pre name="code" class="java">class Outer
{
int x=3;
void method(final int a) //此处变量也要被final修饰;
{
class Inner
{
final int y=4; //final修饰y;
void function()
{
System.out.println(y); //y需要被final修饰
}
}
new Inner().function();
}
}
class Test
{
public static void main(String[] args)
{
new Outer().method(7); //此处7被定义为 final int a
}
}
匿名内部类
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();
}
}
class Test
{
public static void main(String[] args)
{
new Outer().function();
}
}
匿名子类对象的创建如下
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x=3;
public void function()
{
new AbsDemo() //new AbsDemo() 就是父类AbsDemo的匿名子类对象
{
void show()
{
System.out.println("show:"+x);
}
}.show(); //调用匿名内部类
}
}
class Test
{
public static void main(String[] args)
{
new Outer().function();
}
}
匿名内部类命名
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x=3;
public void function()
{
abstract d = new AbsDemo() //给匿名子类对象起个名字
{
void show()
{
System.out.println("show:"+x);
}
void abs()
{
System.out.println("haha");
}
}
d.show(); //可以调用,父类有abs();
//d.abc(); //调用失败。父类没有abs();
}
}
联系
interface Inter
{
void method();
}
class Test
{
static class Inner implements Inter
{
public void method()
{
System.out.println("method run");
}
}
static Inter function()
{
return new Inner();
}
}
class InnerClassTest
{
public static void main(String[] args)
{
Test.function().method();
}
}
//-----------匿名内部类 后如下----------------
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().method();
/*
Test.function() --> Test类中有一个静态的方法function。
.method --> function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
因为只有是Inter类型的对象,才可以调用method方法。
Test.function().method();
相当于
Inter in = Test.fucntion();
in.methood();
*/
}
}