文章转自:http://android.blog.51cto.com/268543/384844/
- 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);
- }
- }
- }
- 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();
- }
- }
- ((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();
- }
- 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 {
- }
- }