package com.sr.mll;
class Food { //外部类
private String name;
int price = 10;
public Food () {
}
public Food (String name) {
this.name = name;
}
public void printFood () {
System.out.println(new com.sr.mll.Food.Fruit().price); //外部类的成员方法调用内部类的成员变量
}
class Fruit { //成员内部类
int price = 11;
public void print () {
int a = Food.this.price; //访问外部类的成员变量
Food.this.printFood(); //访问外部类的成员方法
System.out.println(a + "\t" + Food.this.name);
}
}
}
public class Zh08 {
public static void main(String[] args) {
Food f = new Food("刘备"); //创建外部类对象
Food.Fruit fri = f.new Fruit(); //创建内部类对象
fri.print();
}
}
以上是自己写的一段代码,记录学习情况,如有不对,请多见谅。