内部类
内部类的分类:
- 成员内部类
- 方法内部类
- 局部内部类
- 匿名类
- 静态内部类
1 成员内部类
是在一个类中声明的类,包含内部类的是外围类
成员内部类的访问权限:
- public可以在外围类的外部使用内部类创建对象
- private只能在外围类的内部使用内部类创建对象
创建对象:
- 成员内部类的建立依赖于外围类,在创建内部类的时候必须先用于外部类
public class Outter_v1 {
public static void main(String[] args) {
Outter01 out = new Outter01();
Outter01.Inner in = out.new Inner();
System.out.println(out.a);
System.out.println(out.b);
System.out.println(in.c);
System.out.println(in.d);
}
}
class Outter01{
public int a = 0;
public int b = 1;
public class Inner{
public int c = 2;
public int d = 3;
}
}
public class Outter_v2 {
public static void main(String[] args) {
Outter out = new Outter();
Base in = out.fun();
in.output();
System.out.println(out.a);
System.out.println(out.b);
//System.out.println(in.c); 接口类型只能访问接口中声明的成员
//System.out.println(in.d);
}
}
interface Base{
void output();
}
class Outter{
public int a = 0;
public int b = 1;
public class neuqer implements Base{
public void output(){
System.out.println("我是东秦学生");
}
}
public neuqer fun(){
return new neuqer();
}
}
内部类可以直接访问外部类的成员
public class Outter_v3 {
public static void main(String[] args) {
Outter03 out = new Outter03();
Outter03.Inner in = out.new Inner();
System.out.println(out.a);
System.out.println(out.b);
in.fun();
}
}
class Outter03{
public int a = 0;
public int b = 1;
public class Inner{
public int c = 2;
public int d = 3;
public void fun(){
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
}
2 内部类的内存结构
- 内部类对象和外部类对象分别是两个不同的对象
- 内部类对象依赖于外围类对象,外围类对象是内部类对象的创建者
- 内部类对象中保存着外围类对象的一个引用,可与用来帮助内部类对象访问外围类对象的成员
public class Outter_v4 {
public static void main(String[] args) {
ouuter04 outter = new ouuter04();
ouuter04.inner inner = outter.new inner();
System.out.println(outter.a);
System.out.println(outter.b);
inner.fun();
System.out.println(outter.a);
System.out.println(outter.b);
System.out.println(inner.c);
System.out.println(inner.d);
}
}
class ouuter04{
public int a = 0;
public int b = 1;
public class inner{
public int c = 2;
public int d = 3;
//内部类可以直接修改和访问外部类成员
public void fun(){
a = 4;
b = 5;
}
}
}
3 内部类常见用法
- 隐藏类的实现细节
- 方便实现多重继承
- 实现外围类对于同一个接口不同实现
public class Outter_v5 {
public static void animalCrazy(Animal an){
an.run();
an.say();
}
//加深了对于接口使用的认识
public static void main(String[] args) {
CreateAnimal crea = new CreateAnimal();
Animal dog = crea.createdog();
Animal cat = crea.crearecar();
Animal bird = crea.createbird();
animalCrazy(dog);
animalCrazy(cat);
animalCrazy(bird);
}
}
interface Animal{
public abstract void say();
void run();
}
class CreateAnimal{
private class dog_inter implements Animal {
public void say(){
System.out.println("汪汪汪......");
}
public void run(){
System.out.println("四条腿跑");
}
}
private class cat_inter implements Animal {
public void say(){
System.out.println("喵喵喵......");
}
public void run(){
System.out.println("被追着跑");
}
}
private class bird_inter implements Animal {
public void say(){
System.out.println("咕咕咕......");
}
public void run(){
System.out.println("翅膀飞");
}
}
public dog_inter createdog(){return new dog_inter();}
public cat_inter crearecar(){return new cat_inter();}
public bird_inter createbird(){return new bird_inter();}
}
}
}
public dog_inter createdog(){return new dog_inter();}
public cat_inter crearecar(){return new cat_inter();}
public bird_inter createbird(){return new bird_inter();}
}