【Java】内部类及其调用方法
来源:互联网 发布:阿里云cdn和加带宽 编辑:程序博客网 时间:2024/05/16 17:11
一、static内部类的static方法
public class Test0719_Inner_Test {public static void main(String[] args) {//static内部类的static方法。。。Outter.InnerStatic.InnerNameee();System.out.println(Outter.InnerStatic.jjj);System.out.println();}}class Outter {String i = "非static"; static String j = "static";static class InnerStatic{static String jjj = "内部类的static"; public static void InnerNameee() {System.out.println("static内部类的static方法。。。");//static内部类访问外部类的static成员System.out.println(j);//static内部类访问外部类的非static成员System.out.println(new Outter().i);}}}
运行结果:
static内部类的static方法。。。
static
非static
内部类的static
static
非static
内部类的static
二、static内部类的非static方法
public class Test0719_Inner_Test {public static void main(String[] args) {//static内部类的非static方法Outter.InnerStatic inner = new Outter.InnerStatic();inner.InnerName();System.out.println(inner.iii);System.out.println();}}class Outter {String i = "非static"; static String j = "static";static class InnerStatic{String iii = "内部类的非static";public void InnerName() {System.out.println("static内部类的非static方法");//static内部类访问外部类的static成员System.out.println(j);//static内部类访问外部类的非static成员System.out.println(new Outter().i);}}}
运行结果:
static内部类的非static方法
static
非static
内部类的非static
static
非static
内部类的非static
三、非static内部类的非static方法
public class Test0719_Inner_Test {public static void main(String[] args) {//非static内部类的非static方法…………Outter.InnerNoStatic innerNoStatic = new Outter().new InnerNoStatic();innerNoStatic.InnerNoName();System.out.println(innerNoStatic.iiiNoStatic);}}class Outter {String i = "非static"; static String j = "static";class InnerNoStatic{int iiiNoStatic = 99776;public void InnerNoName() {System.out.println("非static内部类的非static方法…………");//非static内部类访问外部类的static成员System.out.println(Outter.j);//非static内部类访问外部类的非static成员System.out.println(i);}}}
运行结果:
非static内部类的非static方法…………
static
非static
99776
static
非static
99776
四、综合练习
package com.acc;public class Test0719_Inner2 {String str = "孙洋洋";class Text2 {int i = 1;void m2() {System.out.println(new Test0719_Inner2().str);}class Text3 {int k = 3;public void m1() {System.out.println("m1的"+ new Test0719_Inner2().str);}}}public static void main(String[] args) {Test0719_Inner2.Text2 text2 = new Test0719_Inner2().new Text2();text2.m2();System.out.println();Test0719_Inner2.Text2.Text3 text3 = new Test0719_Inner2().new Text2().new Text3();text3.m1();}}
运行结果:
孙洋洋
m1的孙洋洋
m1的孙洋洋
五、汇总
package com.test;public class Test0719_Inner_Test {public static void main(String[] args) {//static内部类的static方法。。。Outter.InnerStatic.InnerNameee();System.out.println(Outter.InnerStatic.jjj);System.out.println();//static内部类的非static方法Outter.InnerStatic inner = new Outter.InnerStatic();inner.InnerName();System.out.println(inner.iii);System.out.println();//非static内部类的非static方法…………Outter.InnerNoStatic innerNoStatic = new Outter().new InnerNoStatic();innerNoStatic.InnerNoName();System.out.println(innerNoStatic.iiiNoStatic);}}class Outter {String i = "非static"; static String j = "static";static class InnerStatic{String iii = "内部类的非static";static String jjj = "内部类的static"; public void InnerName() {System.out.println("static内部类的非static方法");//static内部类访问外部类的static成员System.out.println(j);//static内部类访问外部类的非static成员System.out.println(new Outter().i);}public static void InnerNameee() {System.out.println("static内部类的static方法。。。");//static内部类访问外部类的static成员System.out.println(j);//static内部类访问外部类的非static成员System.out.println(new Outter().i);}}class InnerNoStatic{int iiiNoStatic = 99776;public void InnerNoName() {System.out.println("非static内部类的非static方法…………");//非static内部类访问外部类的static成员System.out.println(Outter.j);//非static内部类访问外部类的非static成员System.out.println(i);}}}
运行结果:
static内部类的static方法。。。
static
非static
内部类的static
static内部类的非static方法
static
非static
内部类的非static
非static内部类的非static方法…………
static
非static
99776
static
非static
内部类的static
static内部类的非static方法
static
非static
内部类的非static
非static内部类的非static方法…………
static
非static
99776
- 【Java】内部类及其调用方法
- java静态方法中调用内部类
- Java main方法调用内部类
- Java静态方法中调用内部类
- JAVA - - - - 内部类的方法(外部调用内部类的方法)
- java方法内部类
- Java:方法内部类
- JAVA中的内部类--方法内部类
- java内部类之方法内部类
- 关于java成员内部类(普通内部类)在main方法中调用
- Java调用内部类成员函数的一个方法
- java中匿名内部类的构造方法调用
- java如何在主类方法调用内部类
- java如何在主类方法调用内部类
- java 静态方法调用非静态内部类报错
- Java private和protected修饰的内部类调用方法
- 在C++中调用JAVA的内部类方法
- Java内部类的调用
- cvSetCaptureProperty
- 解决“adb server is out of date. killing... ”的办法
- eclipse常用方法
- 2013年暑假ACM训练心情
- 怎样将Flash Builder4集成到Myeclipse ...
- 【Java】内部类及其调用方法
- drupal ubercart smtp发送不出去邮件
- EAS开发小工具
- 模型链接
- 使用ViewPager模拟实现应用程序启动界面(二)
- hdu——2955(dp之01背包,但需要转化下思维)
- RTTI、虚函数和虚基类的实现方式、开销分析及使用指导
- 黑马程序员——java IO输入与输出
- python 之 getopt的使用