内部类
内部类可使用访问修饰符
public class Food {
//内部类
public class Banana{
}
}
获取内部类的引用
public class Food {
Banana banana(){
return banana();
};
//内部类
public class Banana{
}
}
内部类的继承
/*
*外部类不能继承内部类
*/
public class Food {
//内部类可以继承外部类
public class Banana extends Fruit{
}
//内部类可以继承本类的内部类
private class Apple extends Banana{
}
}
调用private修饰的内部类的方法
public class Food {
void eat(){
Apple apple =new Apple();
}
//私有类
private class Apple{
Apple(){
System.out.println("吃苹果");
}
}
//测试
public static void main(String[] args) {
Food food = new Food();
food.eat();
}
}
输出结果:
吃苹果
Process finished with exit code 0