java(13)内部类
生活随笔
收集整理的這篇文章主要介紹了
java(13)内部类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
內部類就是定義在類內部的類,可以使java具備為普通類實現多重繼承的能力。
非靜態內部類,局部內部類,靜態內部類,匿名內部類
非靜態內部類:
?
package 內部類;class Wai5 {int i = 8;class Nei{int i = 9;public void myVoid(){System.out.println("內部類的成員變量值為:"+this.i);System.out.println("外部類的成員變量值為:"+Wai5.this.i);}} }public class NeiBuLei5 {public static void main(String[] args) {// TODO Auto-generated method stubWai5 w = new Wai5();Wai5.Nei wn2 = w.new Nei();wn2.myVoid();}}局部內部類:
package 內部類;class WaiBu {public void myVoid(){final int i=9;class Nei{public void myNeiVoid(){System.out.println("外部類的局部變量值:"+i);}}Nei n = new Nei();n.myNeiVoid();} }public class JuBuLei {public static void main(String[] args) {// TODO Auto-generated method stubWaiBu w = new WaiBu();w.myVoid();}}靜態內部類:
package 內部類; class WaiBu1 {static class NeiBU{int i = 1;}public void myVoid(){NeiBU n = new NeiBU();int ii = n.i;System.out.println("靜態內部類的變量值為:" +ii);} } public class 靜態局部類 {public static void main(String[] args) {// TODO Auto-generated method stubWaiBu1 w = new WaiBu1();w.myVoid();}}匿名內部類:
package 內部類;class NeiFather {int i;public void myVoid(){System.out.println("這是內部類的父類方法");} }public class NiMing {public static void main(String[] args) {// TODO Auto-generated method stubNeiFather nf = new NeiFather(){{i=9;}public void myVoid(){System.out.println("匿名內部類成員變量的值為: "+i);}};nf.myVoid();}}?
轉載于:https://www.cnblogs.com/yanmantianxia/p/5471694.html
總結
以上是生活随笔為你收集整理的java(13)内部类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 随机洗牌
- 下一篇: 59. Spiral Matrix II