1、局部内部类
比如:
2、匿名内部类
package com.lwx.inner; public class AnonymousInnerClass { public static void main(String[] args) { Outer02 outer02 = new Outer02(); outer02.m1(); } } class Outer02{ private int n1 = 20; void m1(){ System.out.println("Outer02 m1()..."); IA tiger= new IA(){ @Override public void cry() { System.out.println("老虎嗷嗷叫..."); } }; IA dog = new IA() { @Override public void cry() { System.out.println("小狗汪汪叫..."); } }; tiger.cry(); dog.cry(); Animal cat = new Animal() { @Override void eat() { System.out.println("小猫吃鱼。。。"); } }; cat.eat(); } } interface IA{ public void cry(); } abstract class Animal{ abstract void eat(); }
3、成员内部类
package com.lwx.inner; /** * @author: lwx * @date: 2023-03-09 */ public class MemberInner { public static void main(String[] args) { Outer03 outer03 = new Outer03(); Outer03.Inner03 inner03 = outer03.getInner03(); inner03.say(); outer03.t1(); } } class Outer03{ private int n1 = 10; public String name = "张三"; private void hi(){ System.out.println("hi()方法"); } public class Inner03{ private int n1 = 100; private int sal = 200; public void say(){ System.out.println("n1="+n1+" name="+name+" 外部类的n1"+Outer03.this.n1); hi(); } } public Inner03 getInner03(){ return new Inner03(); } public void t1(){ Inner03 inner03 = new Inner03(); inner03.say(); System.out.println(inner03.sal); } }
4、静态内部类的使用
package com.lwx.inner; /** * @author: lwx * @date: 2023-03-09 15:53 */ public class StaticInner { public static void main(String[] args) { Outer04.inner04 inner04 = new Outer04.inner04(); inner04.say(); System.out.println("==========="); Outer04 outer04 = new Outer04(); Outer04.inner04 inner041 = outer04.getInner04(); inner041.say(); } } class Outer04{ private int n1 = 10; public static String name = "张三"; private static void cry(){ System.out.println("cry()方法..."); } static class inner04{ private static String name = "我是静态内部类"; public void say(){ System.out.println("name="+name+" 外部类的name="+Outer04.name); cry(); Outer04 outer04 = new Outer04(); System.out.println(outer04.n1); } } public void m1(){ inner04 inner04 = new inner04(); inner04.say(); } public inner04 getInner04(){ return new inner04(); } }
标签:Java,内部,--,void,System,inner04,new,public,out From: https://www.cnblogs.com/timothygrass/p/17628259.html