• 17.方法内部类[20220627]


    方法内部类---就类似于类中的局部变量

    将一个java类定义在另一个类的方法中
    1.我们可以在一个java类中的任意一个方法中定义方法内部类
    2.定义在方法中的方法内部类不能使用任何访问限制修饰符

    1. package com.wangxing.test1;
    2. public class Hello {
    3. public Hello(){
    4. class TestWorld{
    5. }
    6. }
    7. public void shiliMethod(){
    8. class World{
    9. }
    10. }
    11. public static void staticMethod(){
    12. class MyWorld{
    13. }
    14. }
    15. }

    方法内部类中可以定义实例变量/实例方法,构造方法,不能有静态变量/静态方法。

    1. package com.wangxing.test1;
    2. public class Hello1 {
    3. public void shiliMethod(){
    4. class World{
    5. public String worldshili="方法内部类的实例变量";
    6. //方法内部类中不能有静态变量
    7. //public static String worldstatic="方法内部类的静态变量";
    8. public World(){
    9. System.out.println("方法内部类的构造方法");
    10. }
    11. public void wordMethod(){
    12. System.out.println("方法内部类的实例方法");
    13. }
    14. //方法内部类中不能有静态方法
    15. //public static void wordStaticMethod(){
    16. // System.out.println("方法内部类的静态方法");
    17. //}
    18. }
    19. }
    20. }

    方法内部类中可以访问方法内部类中的元素

    1.对构造方法的访问,通过new方法
    2.对实例变量/实例方法的访问,this.实例变量/实例方法,对象.实例变量/实例方法,this和对象可以省略.

    1. package com.wangxing.test1;
    2. public class Hello2 {
    3. public void shiliMethod(){
    4. class World{
    5. public String worldshili="方法内部类的实例变量";
    6. public World(){
    7. System.out.println("方法内部类的构造方法");
    8. }
    9. public World(String name){
    10. System.out.println("方法内部类的构造方法");
    11. World w1=new World();
    12. System.out.println(this.worldshili);
    13. System.out.println(w1.worldshili);
    14. System.out.println(worldshili);
    15. this.wordMethod1();
    16. w1.wordMethod1();
    17. wordMethod1();
    18. }
    19. public void wordMethod1(){
    20. System.out.println("方法内部类的实例方法");
    21. }
    22. public void wordMethod2(){
    23. System.out.println("方法内部类的实例方法");
    24. World w1=new World();
    25. System.out.println(this.worldshili);
    26. System.out.println(w1.worldshili);
    27. System.out.println(worldshili);
    28. this.wordMethod1();
    29. w1.wordMethod1();
    30. wordMethod1();
    31. }
    32. }
    33. }
    34. }

    方法内部类中可以访问定义该方法内部类的方法里的局部变量

    方法内部类中可以访问定义该方法内部类的方法里的局部变量,方法里的局部变量默认final修饰符修饰,为常量,可以直接变量名称访问。

    1. package com.wangxing.test1;
    2. public class Hello3 {
    3. public void shiliMethod(){
    4. //默认使用final修饰符修饰的变量
    5. final String jububialiang="局部变量";
    6. //String jububialiang="局部变量";
    7. class World{
    8. public String worldshili="方法内部类的实例变量";
    9. public World(){
    10. System.out.println("方法内部类的构造方法");
    11. //jububialiang="方法局部变量";
    12. System.out.println(jububialiang);
    13. }
    14. public void wordMethod1(){
    15. System.out.println("方法内部类的实例方法");
    16. //jububialiang="方法局部变量";
    17. System.out.println(jububialiang);
    18. }
    19. }
    20. }
    21. }

    方法内部类中可以访问外部类中的元素

    1.对构造方法的访问,通过new访问
    2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,对象/外部类类名.this可以省略。
    3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,对象/外部类类名.this/外部类类名可以省略。

    1. package com.wangxing.test1;
    2. public class Hello4 {
    3. public String waishili="外部类的实例变量";
    4. public static String waistatic="外部类的静态变量";
    5. public Hello4(){
    6. System.out.println("外部类的构造方法");
    7. }
    8. public void waiShiLiMethod(){
    9. System.out.println("外部类的实例方法");
    10. }
    11. public static void waiStaticMethod(){
    12. System.out.println("外部类的静态方法");
    13. }
    14. public void shiliMethod(){
    15. final String jububialiang="局部变量";
    16. class World{
    17. public String worldshili="方法内部类的实例变量";
    18. public World(){
    19. System.out.println("方法内部类的构造方法");
    20. //1.对构造方法的访问,通过new访问
    21. Hello4 h4=new Hello4();
    22. //2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,
    23. //对象/外部类类名.this可以省略。
    24. System.out.println(h4.waishili);
    25. System.out.println(Hello4.this.waishili);
    26. System.out.println(waishili);
    27. h4.waiShiLiMethod();
    28. Hello4.this.waiShiLiMethod();
    29. waiShiLiMethod();
    30. //3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,
    31. //对象/外部类类名.this/外部类类名可以省略。
    32. System.out.println(h4.waistatic);
    33. System.out.println(Hello4.this.waistatic);
    34. System.out.println(Hello4.waistatic);
    35. System.out.println(waistatic);
    36. h4.waiStaticMethod();
    37. Hello4.this.waiStaticMethod();
    38. Hello4.waiStaticMethod();
    39. waiStaticMethod();
    40. }
    41. public void wordMethod1(){
    42. System.out.println("方法内部类的实例方法");
    43. //1.对构造方法的访问,通过new访问
    44. Hello4 h4=new Hello4();
    45. //2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,
    46. //对象/外部类类名.this可以省略。
    47. System.out.println(h4.waishili);
    48. System.out.println(Hello4.this.waishili);
    49. System.out.println(waishili);
    50. h4.waiShiLiMethod();
    51. Hello4.this.waiShiLiMethod();
    52. waiShiLiMethod();
    53. //3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,
    54. //对象/外部类类名.this/外部类类名可以省略。
    55. System.out.println(h4.waistatic);
    56. System.out.println(Hello4.this.waistatic);
    57. System.out.println(Hello4.waistatic);
    58. System.out.println(waistatic);
    59. h4.waiStaticMethod();
    60. Hello4.this.waiStaticMethod();
    61. Hello4.waiStaticMethod();
    62. waiStaticMethod();
    63. }
    64. }
    65. }
    66. }

    定义方法内部类的方法中,可以访问方法内部类的元素

    1.对构造方法的访问,通过new.
    2.对实例元素的访问,只能对象访问.

    1. package com.wangxing.test1;
    2. public class Hello5 {
    3. public void shiliMethod(){
    4. class World{
    5. public String worldshili="方法内部类的实例变量";
    6. public World(){
    7. System.out.println("方法内部类的构造方法");
    8. }
    9. public void wordMethod1(){
    10. System.out.println("方法内部类的实例方法");
    11. }
    12. }
    13. //访问方法内部类中的构造方法,new
    14. World w=new World();
    15. //只能对象访问,实例变量和实例方法
    16. System.out.println(w.worldshili);
    17. w.wordMethod1();
    18. }
    19. }

    外部类中不能访问方法内部类中的元素,方法内部类就相当于是方法中的一个局部变量

  • 相关阅读:
    Maven jar 的查找及依赖版本确定
    LAMMPS小技巧
    反复重购,资金严重浪费?企业资产需要这样管理
    项目分享:新年可以做的副业项目,红包封面制作
    剑指office--字符串
    SwiftUI 如何动态开始和停止播放永久重复(repeatForever)动画
    【设计模式之模板方法模式 -- C++】
    三翼鸟三周年:三次升级,全面引领
    拜托,我也不想跪着的,但这份redis深度笔记也太牛了
    Intellij 自定义MyBatis映射文件模板
  • 原文地址:https://blog.csdn.net/guizhaiteng/article/details/125480941