public class Circle {
private double radius = 0;
public static int count = 1;
public Circle(double radius) {
this.radius = radius;
}
public Draw getInstance(){
return new Draw();
}
class Draw { //成员内部类
private int count=2;
public void drawSahpe() {
System.out.println(radius); //外部类的private成员
System.out.println(Circle.this.count); //外部类的静态成员
}
}
//局部内部类
public void getWoman(){
class Woman extends People{
private String name="woman";
public void print(){
System.out.println("局部内部类: "+name);
}
}
Woman woman=new Woman();
woman.print();
}
//静态内部类
public static class man{
public void print(){
System.out.println("静态内部类");
}
}
//匿名内部类
public void get(){
//匿名内部类是没有名字的类
//多态,本身就继承了Thread或者实现了一个Runnable接口,通过重写方法实现某种执行逻辑
new Thread(){
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread: "+Thread.currentThread().getName()+" 第"+i+"次");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
Runnable run=new Runnable(){
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("runnable: "+Thread.currentThread().getName()+" 第"+i+"次");
}
}
};
new Thread(run).start();
}
}
public class People {
public People(){}
}
public class TestDemo {
public static void main(String[] args) {
Circle circle=new Circle(4);
//成员内部类的两种调用方式
// Circle.Draw draw = circle.new Draw();
Circle.Draw draw = circle.getInstance();
draw.drawSahpe();
//局部内部类的调用
circle.getWoman();
//静态内部类的调用
Circle.man man = new Circle.man();
man.print();
//匿名内部类的调用
circle.get();
}
}
静态内部类和成员内部类的区别:
匿名内部类是否能够继承其他的类或者实现其他的接口: