- package com.itheima;
- //定义一个外部类
- class Outer{
- private String str = "itcast";
- //定义一个内部类
- class Inner{
- private String str = "itheima";
- //打印外部类str
- public void printOuterStr(){
- System.out.println(Outer.this.str);
- }
- //打印内部类str
- public void printInnerStr(){
- System.out.println(str);
- }
- }
- }
- //定义一个外部类
- //class Outer{
- // private static String str = "itcast";
- // 定义一个静态内部类
- // public static class Inner{
- // private String str = "itheima";
- // public Inner(){}
- // //打印外部类str
- // public void printOuterStr(){
- // System.out.println(Outer.str);
- // }
- // //打印内部类str
- // public void printInnerStr(){
- // System.out.println(str);
- // }
- // }
- //
- //}
- public class Test6 {
-
- public static void main(String[] args){
- // 创建内部类实例
- Outer.Inner in = new Outer().new Inner();
- //打印内部类str
- in.printInnerStr();
- //打印外部类str
- in.printOuterStr();
- }
- }
复制代码 |