企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 内部类详解 **内部类简介** ~~~java /** * 在java文件中只能存在一个public修饰的类,但是可以存在多个类 * 外部类 */ public class JavaDemo { public static void main(String[] args) { Outer out = new Outer(); out.fun(); } } /** * 该类不属于内部类,仅仅只是没有被public修饰 * Outer相对于内部类来讲,是外部类 */ class Outer { //定义私有 成员属性 private String str = "Hello,world"; //定义普通方法 public void fun() { Inner in = new Inner();//实例化内部类对象 in.print();//通过内部类对象调用内部类方法 } //定义内部类,类名也要大写 class Inner { int x = 10; //定义内部类方法 public void print() { //Outer.this.str 可以读作 Outer类的对象的str属性 System.out.println(Outer.this.str);//要打印Outer类的属性 } } } ~~~ **成员内部类** ~~~java public class JavaInnerClass1 { public int a = 10; protected int b = 10; private int c = 10; int d = 10; public static int e = 10; final int f = 10; public static final int G = 10; public String fun2() { return "普通方法"; } public static String fun3() { return "静态方法"; } class Inner{//成员内部类-没有用static修饰,位于外部类的内部 int b1 = b; int c1 = c; int d1 = d; int e1 = e; int f1 = f; /*static*/ int g1 = G;//不能定义static成员 String s1 = fun2(); String s2 = fun3(); public static final int X = 10; int a = 20; int a2 = this.a; int a3 = JavaInnerClass1.this.a; } Inner in = new Inner();//创建外部类的属性,在外部类中调用内部类实例化对象还和之前一样 public void fun() { Inner i = new Inner();//不需要创建外部类实例 } public static void fun1() { /** * 内部类创建实例的语法: * [外部类.]内部类 变量名 = new 外部类构造器().new 内部类构造器(); */ JavaInnerClass1.Inner i = new JavaInnerClass1().new Inner();//需要先创建外部类的实例,再创建内部类的实例 } public static void main(String[] args) { JavaInnerClass1.Inner i = new JavaInnerClass1().new Inner(); System.out.println(i.b1); System.out.println(i.c1); System.out.println(i.d1); System.out.println(i.e1); System.out.println(i.f1); System.out.println(i.g1); System.out.println(i.s1); System.out.println(i.s2); System.out.println("=========="); System.out.println(i.a); System.out.println(i.a2); System.out.println(i.a3); } } ~~~ **静态内部类** ~~~java public class Outer2 { Inner in = new Inner();//在外部类中不需要使用外部类引导 int x = 5; static int y = 5; public void fun() { //a = 5;在外部类中不可以直接访问内部类的成员属性 //b = 5;在外部类中不可以直接访问内部类的静态属性 Inner.b = 2; } static class Inner {//静态内部类-使用static修饰,位于外部类中 int a = 10;//成员属性 static int b = 10;//静态属性 Outer2 o = new Outer2(); int t = o.x; int m = y; } public static void main(String[] args) { OutClass ot = new OutClass(); ot.fun2(); System.out.println("OntClass " + ot.in.a); System.out.println("OntClass " +Inner.b); } } class OutClass { /** * 静态内部类实例创建语法: * 外部类.内部类 变量名 = new 外部类.内部类构造器(); */ Outer2.Inner in = new Outer2.Inner(); public void fun2() { //静态内部类中静态属性的访问语法:外部类.内部类.属性 Outer2.Inner.b = 5; in.a = 3; } } ~~~ **方法内部类** ~~~java public class Outer3 { public int a = 5; static int b = 5; private int c = 5; //Inner in = new Inner(); //Outer3.Inner in = new Outer3.Inner(); //Outer3.Inner in = new Outer3().new Inner(); public void fun(int x) { int a = 10; /** * 方法内部类只能在方法中被使用 */ class Inner {//方法内部类-不可以用static以及访问控制符修饰,位于方法中 int a1 = a; int b1 = b; int c1 = c; int d = x; int e = Outer3.this.a; } Inner in = new Inner(); System.out.println(in.a1); System.out.println(in.b1); System.out.println(in.c1); System.out.println(in.d); System.out.println(in.e); } public static void main(String[] args) { Outer3 ot3 = new Outer3(); ot3.fun(3); } } ~~~ **匿名内部类** ~~~java public class Outer4 { /** * 继承一个类 * 实现一个接口 */ public static void main(String[] args) { Out ot = new Out() {//new Out(){} 是一个匿名类继承了Out类 void show() { System.out.println("正在调用匿名内部类的show()方法"); } }; ot.show(); } } class Out { void show() { System.out.println("正在使用out类的show()方法"); } } ~~~ ~~~java public class ListSort { public static void main(String[] args) { Person xs = new Person("徐爽",23); Person xcl = new Person("徐春林",22); Person gjn = new Person("高峻楠",24); List<Person> list = new ArrayList<Person>(); list.add(xs); list.add(xcl); list.add(gjn); System.out.println(list); Collections.sort(list, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { int age1 = o1.age; int age2 = o2.age; int result = age1 - age2; return result; } }); System.out.println(list); } } class Person { public String name; public int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } ~~~