一、内部类
(一)内部类定义
内部类:将一个类定义到另一个类的内部,就称之为内部类
根据内部类定义的位置不同,可以分为成员内部类,和局部内部类
class B{
//成员内部类
class A {
}
public void show(){
//局部内部类
class C{
}
}
}
(二)成员内部类的常见修饰符及应用
A:成员内部类的修饰符:
private 为了保证数据的安全性
static 为了方便访问数据
注意事项:
a:静态内部类访问的外部类数据必须用静态修饰。
b: 成员方法可以是静态的也可以是非静态的
B:成员内部类被静态修饰后的访问方式是:
格式: 外部类名.内部类名 对象名 = new 外部类名.内部类名();
C: 1、 可以直接访问外部类的成员
2、 可以创建内部类对象,通过对象调用内部类方法,来使用局部内部类功能
3、局部内部类访问局部变量必须用final修饰
为什么呢?
因为局部变量会随着方法的调用完毕而消失,这个时候,局部对象并没有立马从堆内存中消失,还要使用那个变量。
为了让数据还能继续被使用,就用fianl修饰,这样,在堆内存里面存储的其实是一个常量值。
// 外部类
class Outer {
// 成员变量
private int a = 45 ;
// 成员方法
public void show() {
// 定义一个局部变量
final int b = 45 ;//JDK1.7之前要加final不然报错 但是在JDK1.8 不用加 不会报错
// 局部内部类
class Inner {
public void method(){
System.out.println(a) ;
System.out.println(b) ;
}
}
// 创建对象
Inner i = new Inner() ;
// 调用放
i.method() ;
}
}
// 测试类
class InnerClassDemo6 {
public static void main(String[] args) {
// 创建Outer的对象
Outer out = new Outer() ;
// 调用show方法
out.show() ;
}
}
(三)匿名内部类的格式和理解
A:匿名内部类: 就是局部内部类的简化写法。
B:前提: 存在一个类或者接口;这里的类可以是具体类也可以是抽象类。
C:格式:
new 类名或者接口名(){
重写方法;
} ;
D:本质是什么呢?
是一个继承了该类或者实现了该接口的子类匿名对象。
(四)(匿名内部类的方法调用
public class MyTest {
public static void main(String[] args) {
Outer outer = new Outer();
System.out.println(outer.num);
outer.outerShow();
System.out.println("-----------------------");
//访问成员内部类的属性和功能
//需要创建成员内部类的对象
Outer.Inner inner=new Outer().new Inner();
System.out.println(inner.b);
inner.innerShow();
//内部类的特点,他可以直接访问外部类的成员,包括私有的
//外部类要访问内部类的成员,要创建内部类的对象
}
}
public class Outer {
int num=100;
private int a=10;
class Inner{
int b=10;
public void innerShow(){
System.out.println("这是内部类的show方法");
System.out.println(num);
System.out.println(a);
outerShow2();
}
public void innerShow2() {
System.out.println("这是内部类的show2方法");
}
}
public void outerShow(){
System.out.println("这是外部类的show方法");
}
private void outerShow2() {
System.out.println("这是外部类的show方法");
}
public void outerTest(){
Inner inner = new Inner();
System.out.println(inner.b);
inner.innerShow2();
}
}
(五)匿名内部类中this关键字使用
interface Inter {
public static final int a = 23 ;
}
public class Test {
public static void main(String[] args) {
new Inter() {
public void show() {
// 这个this表示的是匿名内部类的这个对象
System.out.println(this.a);
}
}.show();
}
(六)类中定义接口
class Computer {
// 内存条的接口
private interface Memory {
// 添加一个抽象方法
public abstract void showMemory(String memory) ;
}
public void appMemory(String memory){
new Memory() {
public void showMemory(String memory){
System.out.println("内存为" + memory) ;
}
}.showMemory(memory) ;
}
}
// 具体类
私有的东西不能被外界访问
class MemoryImpl implements Computer.Memory {
// 添加一个方法
public void showMemory(String memory){
System.out.println("内存为" + memory) ;
}
}
// 测试类
class InnerInterfaceDemo {
public static void main(String[] args) {
// 匿名内部类的方式
私有的东西不能被外界访问
new Computer.Memory() {
public void showMemory(String memory){
System.out.println("内存为" + memory) ;
}
}.showMemory("8G") ;
// 创建Computer的对象
Computer cp = new Computer() ;
// 调用appMemory
cp.appMemory("32G") ;
}
}
(七)匿名内部类
//匿名内部类:他是局部内部类的简写形式
//匿名内部类,本质上是一个对象,是谁的对象,是实现了该接口,或继承了该抽象类的子类对象
//匿名内部类
public class MyTest {
public static void main(String[] args) {
//MyInterface myInterface= new MyClass();
//myInterface.show();
new MyInterface() {
public void show() {
System.out.println("我重写了接口中的抽象方法");
}
}.show();
System.out.println("-----------------");
new MyInterface(){
public void show() {
System.out.println("又是一个匿名内部类,重写了接口中的show方法");
}
}.show();
}
}
interface MyInterface {
void show();
}