将一个java类定义在另一个类的方法中
1.我们可以在一个java类中的任意一个方法中定义方法内部类
2.定义在方法中的方法内部类不能使用任何访问限制修饰符
- package com.wangxing.test1;
- public class Hello {
- public Hello(){
- class TestWorld{
-
- }
- }
- public void shiliMethod(){
- class World{
-
- }
- }
- public static void staticMethod(){
- class MyWorld{
-
- }
- }
- }
方法内部类中可以定义实例变量/实例方法,构造方法,不能有静态变量/静态方法。
- package com.wangxing.test1;
- public class Hello1 {
- public void shiliMethod(){
- class World{
- public String worldshili="方法内部类的实例变量";
- //方法内部类中不能有静态变量
- //public static String worldstatic="方法内部类的静态变量";
- public World(){
- System.out.println("方法内部类的构造方法");
- }
- public void wordMethod(){
- System.out.println("方法内部类的实例方法");
- }
- //方法内部类中不能有静态方法
- //public static void wordStaticMethod(){
- // System.out.println("方法内部类的静态方法");
- //}
- }
- }
- }
1.对构造方法的访问,通过new方法
2.对实例变量/实例方法的访问,this.实例变量/实例方法,对象.实例变量/实例方法,this和对象可以省略.
- package com.wangxing.test1;
- public class Hello2 {
- public void shiliMethod(){
- class World{
- public String worldshili="方法内部类的实例变量";
- public World(){
- System.out.println("方法内部类的构造方法");
- }
- public World(String name){
- System.out.println("方法内部类的构造方法");
- World w1=new World();
- System.out.println(this.worldshili);
- System.out.println(w1.worldshili);
- System.out.println(worldshili);
- this.wordMethod1();
- w1.wordMethod1();
- wordMethod1();
- }
- public void wordMethod1(){
- System.out.println("方法内部类的实例方法");
- }
- public void wordMethod2(){
- System.out.println("方法内部类的实例方法");
- World w1=new World();
- System.out.println(this.worldshili);
- System.out.println(w1.worldshili);
- System.out.println(worldshili);
- this.wordMethod1();
- w1.wordMethod1();
- wordMethod1();
- }
- }
- }
- }
方法内部类中可以访问定义该方法内部类的方法里的局部变量,方法里的局部变量默认final修饰符修饰,为常量,可以直接变量名称访问。
- package com.wangxing.test1;
- public class Hello3 {
- public void shiliMethod(){
- //默认使用final修饰符修饰的变量
- final String jububialiang="局部变量";
- //String jububialiang="局部变量";
- class World{
- public String worldshili="方法内部类的实例变量";
- public World(){
- System.out.println("方法内部类的构造方法");
- //jububialiang="方法局部变量";
- System.out.println(jububialiang);
- }
- public void wordMethod1(){
- System.out.println("方法内部类的实例方法");
- //jububialiang="方法局部变量";
- System.out.println(jububialiang);
- }
- }
- }
- }
1.对构造方法的访问,通过new访问
2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,对象/外部类类名.this可以省略。
3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,对象/外部类类名.this/外部类类名可以省略。
- package com.wangxing.test1;
-
- public class Hello4 {
- public String waishili="外部类的实例变量";
- public static String waistatic="外部类的静态变量";
- public Hello4(){
- System.out.println("外部类的构造方法");
- }
- public void waiShiLiMethod(){
- System.out.println("外部类的实例方法");
- }
- public static void waiStaticMethod(){
- System.out.println("外部类的静态方法");
- }
-
- public void shiliMethod(){
- final String jububialiang="局部变量";
- class World{
- public String worldshili="方法内部类的实例变量";
- public World(){
- System.out.println("方法内部类的构造方法");
- //1.对构造方法的访问,通过new访问
- Hello4 h4=new Hello4();
- //2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,
- //对象/外部类类名.this可以省略。
- System.out.println(h4.waishili);
- System.out.println(Hello4.this.waishili);
- System.out.println(waishili);
- h4.waiShiLiMethod();
- Hello4.this.waiShiLiMethod();
- waiShiLiMethod();
- //3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,
- //对象/外部类类名.this/外部类类名可以省略。
- System.out.println(h4.waistatic);
- System.out.println(Hello4.this.waistatic);
- System.out.println(Hello4.waistatic);
- System.out.println(waistatic);
- h4.waiStaticMethod();
- Hello4.this.waiStaticMethod();
- Hello4.waiStaticMethod();
- waiStaticMethod();
- }
- public void wordMethod1(){
- System.out.println("方法内部类的实例方法");
- //1.对构造方法的访问,通过new访问
- Hello4 h4=new Hello4();
- //2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,
- //对象/外部类类名.this可以省略。
- System.out.println(h4.waishili);
- System.out.println(Hello4.this.waishili);
- System.out.println(waishili);
- h4.waiShiLiMethod();
- Hello4.this.waiShiLiMethod();
- waiShiLiMethod();
- //3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,
- //对象/外部类类名.this/外部类类名可以省略。
- System.out.println(h4.waistatic);
- System.out.println(Hello4.this.waistatic);
- System.out.println(Hello4.waistatic);
- System.out.println(waistatic);
- h4.waiStaticMethod();
- Hello4.this.waiStaticMethod();
- Hello4.waiStaticMethod();
- waiStaticMethod();
- }
- }
- }
- }
1.对构造方法的访问,通过new.
2.对实例元素的访问,只能对象访问.
- package com.wangxing.test1;
- public class Hello5 {
- public void shiliMethod(){
- class World{
- public String worldshili="方法内部类的实例变量";
- public World(){
- System.out.println("方法内部类的构造方法");
- }
- public void wordMethod1(){
- System.out.println("方法内部类的实例方法");
- }
- }
- //访问方法内部类中的构造方法,new
- World w=new World();
- //只能对象访问,实例变量和实例方法
- System.out.println(w.worldshili);
- w.wordMethod1();
- }
- }
外部类中不能访问方法内部类中的元素,方法内部类就相当于是方法中的一个局部变量