- Java allows a class A to be declared in another class B, then class A is an inner class, and class B is called an outer class
- Classification of internal classes: member internal classes (static, non-static) and local internal classes (in methods, code blocks, and constructors)
- Member inner class:
-
On the one hand, as a member of an external class:
- Call the structure of the external class
- Can be modified by static
- Can be modified by 4 different permissions
-
On the other hand, as a class:
- Properties, methods, constructors, etc. can be defined in the class
- Can be modified by final, which means that this class cannot be inherited. The implication is that it can be inherited without using final
- Can be modified by abstract
-
/**
* @Author: YuShiwen
* @Date: 2020/11/19 10:21 AM
* @Version: 1.0
*/
public class InnerClass {
public static void main(String[] args) {
//创建静态的成员内部类实例:
Student.Maths maths = new Student.Maths();
maths.appraise();
//创建非静态的成员内部类实例:
//错误示范:
//Student.Physics physics = new Student.Physics();
//需要先创建外部类对象
Student student = new Student();
Student.Physics physics = student.new Physics();
physics.appraise();
physics.namesake(18);
}
}
class Student{
String name = "Mr.Yu";
int age = 21;
public void drink(){
System.out.println("口渴了,喝水!");
}
//静态成员内部类:
static class Maths{
String teacher = "Mr.Wu";
int score;
public void appraise(){
System.out.println("该学生成绩优秀,学习刻苦认真!");
//静态成员中,不能调用非静态属性或方法
//drink();
//System.out.println(name);
}
}
//非静态成员内部类
public final class Physics{
String teacher = "Mr.Li";
int age = 48;
int score;
public void appraise(){
System.out.println("该学生成绩优秀,学习刻苦认真!");
drink();
System.out.println(name);
}
//内部类方法的形参、内部类的属性、外部类的属性同名情况
public void namesake(int age){
//内部类方法的形参
System.out.println(age);
//内部类的属性
System.out.println(this.age);
//外部类的属性
System.out.println(Student.this.age);
}
}
//以下都为局部内部类演示
//方法中声明局部内部类
public void localInnerClassDisplay(){
//局部内部类
class LocalInnerClass{
}
}
//代码块中声明局部内部类
{
//局部内部类
class LocalInnerClass{
}
}
//构造器中声明局部内部类
public Student(){
//局部内部类
class LocalInnerClass{
}
}
}
Output result:
该学生成绩优秀,学习刻苦认真!
该学生成绩优秀,学习刻苦认真!
口渴了,喝水!
Mr.Yu
18
48
21
Process finished with exit code 0
- Demonstration of local internal classes:
interface Person{
void eat();
void drink();
}
public class LocalInnerClass {
//返回一个实现了Person接口的类的对象
//方式一:
public Person getPerson1(){
//1.先创建一个实现了Comparable接口的类:局部内部类
class Student implements Person{
@Override
public void eat() {
System.out.println("吃饭!");
}
@Override
public void drink() {
System.out.println("喝牛奶!");
}
}
//2.返回局部内部类对象
return new Student();
}
//返回一个实现了Person接口的类的对象
//方式二:
public Person getPerson2(){
//直接返回一个匿名局部内部类的匿名对象
return new Person(){
@Override
public void eat() {
System.out.println("吃牛肉!");
}
@Override
public void drink() {
System.out.println("喝饮料!");
}
};
}
}
class MainTest{
public static void main(String[] args) {
LocalInnerClass localInnerClass = new LocalInnerClass();
Person person1 = localInnerClass.getPerson1();
Person person2 = localInnerClass.getPerson2();
person1.eat();
person1.drink();
person2.eat();
person2.drink();
}
}
operation result:
吃饭!
喝牛奶!
吃牛肉!
喝饮料!
Process finished with exit code 0