专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java在main中访问内部类、方法等

Java在main中访问内部类、方法等

更新时间:2020-08-04 16:33:59 来源:动力节点 浏览2831次

1.使用静态的属性、方法、内部类

 class A
 {
     static int i = 1;    //    A 类的静态属性
     static void outPut()    //    A 类的静态方法
     {
         System.out.println(i);
     }
     static class B        //    A 类的静态内部类
     {
         void outPut()
         {
             System.out.println("B");
         }
     }
     public static void main(String[] args)
     {
 
         System.out.println(i);       //    调用静态的属性
         outPut();            //    调用静态的方法
         B b = new B();        //    调用静态的内部类
         b.outPut();
     }
 }

2.使用此类的对象名访问

 class A
 {
     int i = 1;    //    属性
     void outPut()    //    方法
     {
         System.out.println(i);
     }
     class B    //    内部类
     {
         void outPut()
         {
             System.out.println("B");
         }
     }
     B newB()            //    (关键)在动态方法中建立 B 的对象
     {
         B b = new B();
         return b;
     }
     public static void main(String[] args)
     {
         A a = new A();
         System.out.println(a.i);        //    调用属性
         a.outPut();        //    调用方法
         B b = a.newB();        //    调用内部类
         b.outPut();
     }
 }

在静态的main中,无法创建非静态的内部类。

Java在main中访问内部类、方法等

以上就是动力节点java培训机构的小编针对“Java在main中访问内部类、方法等”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>