Java SE 第八十四,八十五,八十六讲 Observable类与Observer接口详解,深入理解观察者模式 Java内部类深度剖析及常见使用陷阱 Java SE IO详解...

本文深入解析Java中的四种内部类:静态内部类、成员内部类、局部内部类和匿名内部类的特点与用法,同时介绍了如何在不同场景下创建和使用这些内部类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.内部类:(Inner class):Java中的内部类共分为4中.

2.内部类:Group related classes and thus reduce namespace clutter

defined at a scope smaller than a package

An inner class can be defined inside another class,inside a method,and even as part of an expression.

There are four types of inner classes

a)static Inner classes(also called nested classes)

b)member Inner classes

c)local Inner classes

d)anonymous Inner classes

3.静态内部类(嵌套类)static Inner class

a)The simplest forn of inner class

b)Can’t have the same name as the enclosing class

c)Compiled into a completely separtate .class file from the outer class

编译生成单独的.class文件.

文件名为:外部类名$内部类名.class

d)Can access only static members and methods of the enclosing class, including private static members

只能够访问外部类静态的成员,方法.

e)Create an instance of a static inner class out of enclosing class:

new outerclass.innnerclass();

4.成员内部类:(member innerclass)

Defined in an enclosing class without using the static modifier.定义在一个类的内部,且没有static关键字修饰.

Like instance variables.就像实例变量一样.

Can access all members of the enclosing class

想在内部类中访问外部类的变量,可以使用outerclass.this.变量名.

outerclass.this表示对外部类的引用.

Create an instance within the enclosing class

this.new Innerclass();

Create an instance out of the enclosing class:

(new Outerclass()).new Innerclass;

Access members of the enclosing class with inner classes:

Outerclass.this.member;//内部类引用外部类的成员变量`

总结:成员内部类(member innerclass):可以访问外部类的静态与非静态的方法和变量,生成成员内部类的方法为:

OuterClass.InnerClass inner = new OuterClass().new InnerClass();

5.若想在局部内部类中访问外部类的成员变量,语法为:OuterClass.this.a;

6.局部内部类(Local Innerclass):

Defined within the scope of a method,even smaller blocks within methods.

The least used form of inner class.

Like local variables,can’t be declared public,protected,private and static

Can only access final local variables.

summary:定义在方法当中,只能当问方法中的final类型的变量.

7.匿名内部类(Anonymous Inner Class)

Local inner classes which don’t have class names

No key word class

No key word extends and implements

No constructors

Implicitly extend a superclass or implement an interface

summary:匿名内部类会隐式地继承一个父类或者实现一个接口.

8.Java IO系统:

对于程序语言设计者来说,设计一个令人满意的I/O(输入输出)系统,是件极其困难的任务.-------------Think in Java

9.File类:表示了磁盘上的文件或是目录.

File提供了与平台无关的方法来对磁盘上的文件或目录进行操作.

File类直接处理文件和文件系统

File类没有指定信息怎样从文件读取或向文件存储.

File类描述了文件本身的属性

File对象用来获取或处理与磁盘文件相关的信息,例如权限,时间,日期和目录路径.

File类还可以浏览子目录层次结构.

10.路径分隔符:Java中默认的"\"表示转义字符,表示路径时,使用"\\"表示路径分隔符,也可以使用"/";建议使用"/"以提高可移植性.

转载于:https://www.cnblogs.com/donaldjohn/archive/2011/03/19/1988984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值