Java内部类
看到的几句话:
对于非静态内部类,不能有静态成员,例如变量、方法等。
静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量。
非静态内部类的非静态成员可以访问外部类的非静态变量。
Java内部类总结
1. 成员内部类
public class Outer { public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.print("Outer.new"); inner = outer.getInner(); inner.print("Outer.get"); } // 个人推荐使用getxxx()来获取成员内部类,尤其是该内部类的构造函数无参数时 public Inner getInner() { return new Inner(); } public class Inner { public void print(String str) { System.out.println(str); } } }
2. 局部内部类
public class Parcel4 { public Destination destination(String s) { class PDestination implements Destination { private String label; private PDestination(String whereTo) { label = whereTo; } public String readLabel() { return label; } } return new PDestination(s); } public static void main(String[] args) { Parcel4 p = new Parcel4(); Destination d = p.destination("Tasmania"); } }
public class Parcel5 { private void internalTracking(boolean b) { if (b) { class TrackingSlip { private String id; TrackingSlip(String s) { id = s; } String getSlip() { return id; } } TrackingSlip ts = new TrackingSlip("slip"); String s = ts.getSlip(); } } public void track() { internalTracking(true); } public static void main(String[] args) { Parcel5 p = new Parcel5(); p.track(); } }
3. 嵌套内部类
4. 匿名内部类
((Button) findViewById(R.id.start)).setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { new Thread() { @Override public void run() { // TODO Auto-generated method stub } }.start(); } });
public class Outer { public static void main(String[] args) { Outer outer = new Outer(); Inner inner = outer.getInner("Inner", "gz"); System.out.println(inner.getName()); } public Inner getInner(final String name, String city) { return new Inner() { private String nameStr = name; public String getName() { return nameStr; } }; } } //注释后,编译时提示类Inner找不到 /* interface Inner { String getName(); } */
public class Outer { public static void main(String[] args) { Outer outer = new Outer(); Inner inner = outer.getInner("Inner", "gz"); System.out.println(inner.getName()); } public Inner getInner(final String name, String city) { return new Inner(name, city) { private String nameStr = name; public String getName() { return nameStr; } }; } } abstract class Inner { Inner(String name, String city) { System.out.println(city); } abstract String getName(); }
public class Outer { public static void main(String[] args) { Outer outer = new Outer(); Inner inner = outer.getInner("Inner", "gz"); System.out.println(inner.getName()); System.out.println(inner.getProvince()); } public Inner getInner(final String name, final String city) { return new Inner() { private String nameStr = name; private String province; // 实例初始化 { if (city.equals("gz")) { province = "gd"; }else { province = ""; } } public String getName() { return nameStr; } public String getProvince() { return province; } }; } } interface Inner { String getName(); String getProvince(); }
5.内部类的继承
public class InheritInner extends WithInner.Inner { // InheritInner() 是不能通过编译的,一定要加上形参 InheritInner(WithInner wi) { wi.super(); } public static void main(String[] args) { WithInner wi = new WithInner(); InheritInner obj = new InheritInner(wi); } } class WithInner { class Inner { } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。