No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing
No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing instance of type SmsUtils (e.g. x.new A() where x is an instance of SmsUtils).
今天在寫一個短信發送的工具類時使用到了內部類,在實例化內部類時遇到此錯誤。
SmsResult result = new SmsResult();
沒有可以訪問SmsUtils類型的封閉實例。必須使用封閉的SmsUtils類型的實例來限定分配(例如,x.new A(),其中x是SmsUtils的一個實例)。
SmsResult?是一個(非靜態的)內部類。?正在從靜態上下文創建它。您需要提供一個Server實例作為外部實例。但是,幾乎可以肯定我想讓?SmsResult?成為一個靜態的嵌套類,或者可能是一個外部類。
剛好今天看到一篇文章中提到:?
今天面試我問你static關鍵字有哪些作用,如果你答出static修飾變量、修飾方法我會認為你合格,答出靜態塊,我會認為你不錯,答出靜態內部類我會認為你很好,答出靜態導包我會對你很滿意,因為能看出你非常熱衷研究技術。
給?SmsResult 類的定義添加 static 關鍵字就ok了。
總結
以上是生活随笔為你收集整理的No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python基础学习四 函数
- 下一篇: 【289】◀▶ Python I/O