1.内部类:在一个类中,再定义一个类
2.使用场景:内部类表示的事物时外部类的一部分,内部类单独出现没有任何意义 例如:车和发动机,发动机依赖于车 ,人的心脏
3.内部类的访问特点:
a.内部类可以直接访问外部类的成员,包括私有
b.外部类要访问内部类的成员,必须创建对象
public class Car {
private String brand;
private String carName;
private int carAge;
private String carColor;
class Engine{
private String engineName;
private int engineAge;
public Engine() {
}
public Engine(String engineName, int engineAge) {
this.engineName = engineName;
this.engineAge = engineAge;
}
//内部类可以直接访问外部类的成员,包括私有
public void show(){
System.out.println(carName);
System.out.println(engineName);
}
public String getEngineName() {
return engineName;
}
public void setEngineName(String engineName) {
this.engineName = engineName;
}
public int getEngineAge() {
return engineAge;
}
public void setEngineAge(int engineAge) {
this.engineAge = engineAge;
}
}
public void show(){
System.out.println(brand);
//外部类要访问内部类的成员,必须创建对象
Engine e = new Engine();
System.out.println(e.getEngineName());
}
public Car() {
}
public Car(String brand, String carName, int carAge, String carColor) {
this.brand = brand;
this.carName = carName;
this.carAge = carAge;
this.carColor = carColor;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarAge() {
return carAge;
}
public void setCarAge(int carAge) {
this.carAge = carAge;
}
public String getCarColor() {
return carColor;
}
public void setCarColor(String carColor) {
this.carColor = carColor;
}
}
4.内部类的分类:
a.成员内部类(了解)
b.静态内部类(了解)
c.局部内部类(了解)
d.匿名内部类(掌握)
5.成员内部类(了解)
a.写在成员位置的,属于外部类的成员
//外部类
public class Car {
class Engine{
private String engineName;
private int engineAge;
//成员内部类
public Engine() {
}
public Engine(String engineName, int engineAge) {
this.engineName = engineName;
this.engineAge = engineAge;
}
}
}
b.成员内部类可以被一些修饰符所修饰,比如:private,默认,protected,public,static等
c.获取成员内部类对象
在外部类中编写方法,对外提供内部类的对象(private修饰成员内部类时)
//在外部类中编写方法,对外提供内部类的对象
public Engine getEngine(){
return new Engine();
}
//在外部类中编写方法,对外提供内部类的对象
Car.Engine e2 = new Car().getEngine();
直接创建格式:外部类名.内部类名 对象名 = 外部类对象.内部类对象
Car.Engine e =new Car().new Engine();
d.在成员内部类中,JDK16之前不能定义静态变量,JDK16开始才可以定义静态变量
e.外部类成员变量和内部类成员变量重名时,在内部内如何访问:外部类名.this.变量名
public class Car1 {
private int number = 10;
class Engine{
private int number= 20;
public void show(){
int number= 30;
System.out.println(number);//输出30
System.out.println(this.number);//输出20
System.out.println(Car1.this.number);//输出10
}
}
}
6.静态内部类(了解):一种特殊的成员内部类
a.静态内部类只能访问外部类中的静态变量,如果想访问非静态的需要创建对象
b.创建静态内部类对象的格式:外部类名.内部类名 对象名 = new 外部类名.内部类名();
c.调用非静态方法的格式:先创建对象,用对象调用
d.调用静态方法的格式:外部类名.内部类名.方法名()
//创建静态内部类对象的格式:外部类名.内部类名 对象名 = new 外部类名.内部类名();
Car1.Engine e3 =new Car1.Engine();
//调用非静态方法的格式:先创建对象,用对象调用
e3.show();
//调用静态方法的格式:外部类名.内部类名.方法名()
Car1.Engine.show2();
7.局部内部类(了解):
a.将内部类定义在方法里面就叫局部内部类,类似于方法里面的局部变量
b.外界是无法直接使用,需要在方法内部创建对象并使用
c.该类可以直接访问外部类的成员,也可以访问方法内的局部变量
8.匿名内部类(重要):隐藏了名字的内部类,可以写在成员位置,也可以写在局部位置
a.匿名内部类本质上就是隐藏了名字的内部类
b.格式:
new 类名或者接口名(){
重写方法:
};
c.格式的细节:包含了三个方面:实现/继承关系 方法的重写 创建对象,整体就是一个类的子类对象或者接口的实现类对象
4.使用场景:
当方法的参数是接口或者类时,以接口为例,可以传递这个接口的实现类对象.如果实现类只要使用一次,就可以用匿名内部类简化代码.