Java学习之路--oop--面向对象编程(封装、继承、多态)05

发布时间 2023-09-20 15:36:56作者: 寂灭无言
package com.oop.demo10;

public class Application {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
// inner.getID();
}

//
package com.oop.demo10;

public class Outer {
//局部内部类
public void method(){

class Inner{
public void in(){

}
}
}

private int id;
public void out(){
System.out.println("这是外部类的方法");
}

//成员内部类
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}

//获得外部类的私有属性~方法....
public void getID(){
System.out.println(id);
}
}
}
//静态内部类
//public static class Ier{
//
//}

//一个Java类中可以有多个class类,但是只能有一个public class
class A{
public static void main(String[] args) {

}
}
//
//
package com.oop.demo10;

public class Test {
public static void main(String[] args) {
//没有名字的初始化类,不用将实例保存到变量中~
//匿名内部类
new Apple().eat();

new UserService(){
@Override
public void hello() {

}
};
}
}

class Apple{
public void eat(){
System.out.println("1");
}
}

interface UserService{
void hello();
}