匿名內部類訪問許可權
阿新 • • 發佈:2019-03-03
內部類:抽象類例項化 介面例項化
匿名內部類只能訪問外部的靜態變數 和final修飾的變數
作用:內部類通過該訪問路徑可以進行內部類內部和外部的資料互動,一般與final結合使用比較多
package com.zhiwei.basic; public class AnonClass { private static String name = "Hello Python World!"; public static void main(String[] args) { final String text="Hello Java World!"; //抽象類構建內部類:相當於將抽象類例項化 AbstractClass abstractClass = new AbstractClass() { @Override public void eat() { System.out.println("People is eating......."); System.out.println("static屬性:"+name); System.out.println("final屬性:"+text); } }; abstractClass.eat(); //介面構建內部類 Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Thread is running........."); System.out.println("static屬性:" + name); System.out.println("final屬性:" + text); } }); thread.start(); } } abstract class AbstractClass { public abstract void eat(); }
結果:
People is eating.......
static屬性:Hello Python World!
final屬性:Hello Java World!
Thread is running.........
static屬性:Hello Python World!
final屬性:Hello