JAVA——内部类
2016-04-22 17:26
405 查看
定义:
内部类就是在一个类中再定义一个类。如下面的Inner类。class Outer { private int x = 3; class Inner //内部类 { void function() { System.out.println("inner :"+x); } } void method() { Inner in = new Inner(); in.function(); System.out.println(x); } }
内部类的访问规则:
1、内部类可以直接访问外部类中的成员,包括私有,之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式:外部类名.thisclass Outer { private int x = 3; class Inner //内部类 { int x = 6; void function() { int x = 4; System.out.println("inner :"+Outer.this.x);//标识外部类的this,不加Outer则表示内部类的this. } } void method() { Inner in = new Inner(); in.function(); //System.out.println(x); } }
2、外部类要访问内部类,必须建立内部类对象。
class Outer { private int x = 3; class Inner //内部类 { void function() { System.out.println("inner :"+x); } } 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();
}
}
3、访问格式:
当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中直接建立内部类对象。
格式:
外部类名.内部类名 变量名 = 外部类对象.内部类对象
Outer.Inner in = new Outer().new Inner();
当内部类在成员位置上,就可以被成员修饰符所修饰。
比如,private:将内部类在外部类中进行封装。
static :内部类就具备static的特性
当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限。
注意:内部类中如果定义了静态成员,该内部类必须被静态修饰。
问题:在外部其他类中,如何直接访问静态内部类的非静态成员呢?
new Outer.Inner().function();
class Outer { private static int x = 3; static class Inner //内部类 { void function() { System.out.println("inner :"+x);//标识外部类的this,不加Outer则表示内部类的this. } } }
问题:在外部其他类中,如何直接访问静态内部类的静态成员呢?
Outer.Inner.function();
class Outer { private static int x = 3; static class Inner //内部类 { static void function() { System.out.println("inner :"+x);//标识外部类的this,不加Outer则表示内部类的this. } } }
注意:当内部类中定义了静态成员,该内部类必须为static的。
class Outer { private static int x = 3; static class Inner //内部类 { static void function() { System.out.println("inner :"+x);//标识外部类的this,不加Outer则表示内部类的this. } } }
注意:当外部内中的静态成员访问内部类时,内部类也必须是static的。
class Outer { static class Inner2 //当method()中访问时,该内必须为静态 { void show() { System.out.println("Inner2 show"); } } public static void method() { new Inner2().show(); } }
内部类定义原则:
当描述事物时,事物的内部还有事物,该事物用内部内来描述。因为内部事物在使用外部事物的内容。
class Body() { private class XinZang { } public void show() { new XinZang(); } }
/* 内部类定义在局部时, 1.不可以被成员修饰符修饰 2.可以直接访问外部类中的成员,因为还持有外部类中的引用 但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。 */ class Outer { int x = 3; void method(final int a) { final int y = 4; class Inner { void function() { System.out.println("a:"+a+"\ty:"+y); } } new Inner().function(); } } class InnerClassDemo3 { public static void main(String[] args) { new Outer().method(7); } }
匿名内部类:
1、匿名内部类其实就是内部类的简写格式2、定义匿名内部类的前提:内部类必须是继承一个类或者实现接口
3、匿名内部类的格式:new 父类或者接口(){定义子类的内容}
4、其实匿名内部类就是一个匿名子类对象;而且这个对象有点胖。可以理解为
带内容的对象。
5、匿名内部类中定义的方法最好不要超过3个.
abstract class AbsDemo { abstract void show(); } class Outer { int x = 3; public void function() { //new Inner().show(); new AbsDemo() { void show() { System.out.print("x= "+x); } }.show(); } } class InnerClassDemo4 { public static void main(String[] args) { System.out.println("Hello world!"); new Outer().function(); } }
结果:
Hello world!
x= 3
匿名内部类的应用:
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类型的对象,才可以调用mrthod方法 Test.function().method(); show(new Inter(){ public void method() { System.out.println("method show run"); } }); } public static void show(Inter in) { in.method(); } }
输出结果:
method run
method show run
相关文章推荐
- 使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException
- Spring报错
- java爬虫学习日记1-基本爬虫原理介绍
- Java中如何创建不可变对象
- Spring Transaction配置简介
- WIN8.1 JDK的安装与配置
- [改善Java代码] 枚举项数量限定为64个以内
- Java ArrayList工作原理及实现
- Spring AOP嵌套调用的问题 (同一类方法内调用切面切不到)
- spring mvc 检查添加的某个字段是否在数据库里已经存在
- [置顶] mac下IDEA配置Spring框架+mybatis+maven配置管理的web项目过程详解
- [Drools]JAVA规则引擎 -- Drools
- springmvc中@InitBinder
- java连接mysql实例及数据库操作
- java实现简单的单点登录
- SpringMVC中JSP取不到ModelAndView的数据原因
- SpringMVC @RequestBody 接收Json数组对象
- spring-security api
- Spring @Scheduled
- spring初涉--七大模块