日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

异常: No enclosing instance of type xx is accessible. Must qualify (Java 内部类)

發(fā)布時間:2024/3/12 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异常: No enclosing instance of type xx is accessible. Must qualify (Java 内部类) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

異常

前言. 開發(fā)時候遇到內(nèi)部類的問題.

  • 異常
No enclosing instance of type SparkProgrammingGuide is accessible. Must qualify the allocation with an enclosing instance of type SparkProgrammingGuide (e.g. x.new A() where x is an instance of SparkProgrammingGuide).
  • 代碼如下
// 注意 static class GetLength implements Function<String, Integer> {public Integer call(String s) {return s.length();}}class Sum implements Function2<Integer, Integer, Integer> {public Integer call(Integer a, Integer b) {return a + b;}}// 繼承方法的形式進(jìn)行運(yùn)行.// 特別注意 局部類 No enclosing instance of type XXX is accessible. Must qualify 異常.static int getTotalLengthByExtendsFunction(JavaSparkContext context) {int totalLength = 0;if (null != context) {JavaRDD<String> distFile = context.textFile("data/wordcount/spark-overview.txt");JavaRDD<Integer> linesRDD = distFile.map(new GetLength());totalLength = linesRDD.reduce(new Sum());}return totalLength;}

解決措施

  • 方法1
    將局部類設(shè)置為靜態(tài)的. 類名前添加<static>關(guān)鍵字即可.
static class Sum implements Function2<Integer, Integer, Integer> {public Integer call(Integer a, Integer b) {return a + b;}}
  • 方法2
    通過類名.局部類名訪問即可. 即如下的new ParentClassName().new GetLength().
JavaRDD<Integer> linesRDD = distFile.map(new ParentClassName().new GetLength());
  • 方法3
    不使用局部類即可. 作為非public類型類寫在一個文件即可.

Reference

[1] JAVA在編寫內(nèi)部類時No enclosing instance of type XXX is accessible. Must qualify原因及解決.
[2] No enclosing instance of type 類名 is accessible. Must qualify the allocation with.

總結(jié)

以上是生活随笔為你收集整理的异常: No enclosing instance of type xx is accessible. Must qualify (Java 内部类)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。