TJI读书笔记13-内部类

我最恶心的两部分内容,一个叫lambda表达式,一个叫内部类. 智商有限,一直理解不了…

简单来说,将一个类的定义放在另一个类的内部就是内部类. 内部类的好处在于,将一些逻辑上相关的类组织在一起,并控制位于内部的类的可视性.内部类可以了解外围类,并与之通信. 但是内部类和组合是两个概念,比如将人的大腿定义在人这个类的内部,这就是一个比较合理的设定.如果通过组合,也可以实现,但是总是有那么一丢丢别扭的. 因为大腿脱离了人,几乎就没用任何意义了.

创建内部类

来吧,一码解千愁

1.public class Parcel1 {
2. //a inner class
3. class Contents{
4. private int i = 11;
5. public int value(){
6. return i;
7. }
8. }
9.
10. //a inner class
11. class Destination{
12. private String label;
13. public Destination(String whereTo) {
14. label=whereTo;
15. }
16. String readLabel(){
17. return label;
18. }
19. }
20.
21. public void ship(String dest){
22. Contents c = new Contents();
23. Destination d = new Destination(dest);
24. System.out.println(c.value());
25. System.out.println(d.readLabel());
26. }
27. public static void main(String[] args) {
28. Parcel1 p = new Parcel1();
29. p.ship("USA");
30.
31. }
32.}

这是一个非常简单的应用,就像使用其他普通的类一样去使用内部类.更典型的情况,在外部类中提供一个方法返回一个指向内部类实例的引用.

1.public class Parcel2 {
2. class Contents {
3. private int i = 11;
4.
5. public int value() {
6. return i;
7. }
8. }
9.
10. class Destination {
11. private String label;
12.
13. public Destination(String whereTo) {
14. label = whereTo;
15. }
16.
17. String readLabel() {
18. return label;
19. }
20. }
21.
22. public Destination to(String s) {
23. return new Destination(s);
24. }
25.
26. public Contents contents() {
27. return new Contents();
28. }
29.
30. public void ship(String dest) {
31. Contents c = this.contents();
32. Destination d = this.to(dest);
33. System.out.println(d.readLabel());
34. }
35.
36. public static void main(String[] args) {
37. Parcel2 p1 = new Parcel2();
38. p1.ship("USA");
39.
40. // define reference to inner classes
41. Parcel2 p2 = new Parcel2();
42. Parcel2.Contents c = p2.contents();
43. Parcel2.Destination d = p2.to("UK");
44. System.out.println(c.value());
45. System.out.println(d.readLabel());
46. }
47.}

注意,22行,26行定义了两个方法用来返回内部类的实例. 通过这种方式可以更好的做校验逻辑. 另外,还有42-43行,在外部类的非静态方法之外的地方创建内部类实例的时候,需要用OuterClassName.InnerClassName的方式具体指明对象的类型.

突然想到一个问题,内部类在加载过程中是个什么样子的?

内部类和外部类的关系

从前面一小节,我们可以得知内部类的一个作用:对外隐藏名称空间和组织代码. 这一节唠唠一个更重要的特性,内部类可以无条件的访问外部嵌套类的所有成员.内部类的可视范围是它的直接外嵌类,也就是说内部类其实是拥有两个this指针,一个指向它自己的实例本身,另外一个指向外嵌类的实例.

1.interface Selector {
2. //check if the sequence cursor goto end.
3. boolean end();
4. //return the object where the current cursor pointed
5. Object current();
6. //move the cursor to next element
7. void next();
8.}
9.
10.public class Sequence {
11. private Object[] items;
12. private int next = 0;
13. public Sequence(int size){
14. items = new Object[size];
15. }
16. public void add(Object x){
17. if(next<items.length){
18. items[next++] = x;
19. }
20. }
21. private class SequenceSelector implements Selector{
22. private int i =0;
23. public boolean end(){
24. return i == items.length;
25. }
26. public Object current(){
27. return items[i];
28. }
29. public void next(){
30. if(i<items.length) i++;
31. }
32. }
33.
34. public Selector selector(){
35. return new SequenceSelector();
36. }
37. public static void main(String[] args) {
38. Sequence sequence = new Sequence(10);
39. for(int i =0;i<10;i++){
40. sequence.add(Integer.toString(i));
41. }
42. Selector selector = sequence.selector();
43. while(!selector.end()){
44. System.out.print(selector.current()+" ");
45. selector.next();
46. }
47. }
48.}

这是一个典型的迭代器原型,注意,24,27,30行均访问了外嵌类的成员 items. 这个是可以的. 说明内部类可以访问外嵌类成员.可以说内部类使用外嵌类的成员时,就像访问它自己的成员一样. 也就是说内部类 自动拥有其外嵌类所有成员的访问权,这是怎么做到的呢?埃大爷说,当外嵌类实例创建内部类实例的时候,此内部类对象必定会捕获一个指向外嵌类对象的引用. 当我们访问外嵌类的成员的时候,就是使用那个引用来选择外嵌类的成员. 这一切都是隐式完成的,我们不需要操心. 其实也就是开头的时候说的,内部类会拥有两个this指针.

还有一个地方需要注意,第21行,内部类使用的private修饰符. 这个表示在外嵌类之外,内部类不可见. 也就是说内部类是可以使用private修饰的. 但是不要忘了,普通的类是不可以使用private修饰.

.this和.new

.this

如果需要在内部类中获取外嵌类的引用,可以使用OuterClassName.this的方式来获取.

1.public class DotThis {
2. void f(){
3. System.out.println("DoThis.f()");
4. }
5. public class Inner{
6. public DotThis outer(){
7. return DotThis.this;
8. }
9. }
10. public Inner inner(){
11. return new Inner();
12. }
13. public static void main(String[] args) {
14. DotThis dt = new DotThis();
15. DotThis.Inner dti = dt.inner();
16. dti.outer().f();
17.
18. System.out.println(dt==dti.outer());
19. }
20.}/*output:
21.DoThis.f()
22.true
23.*/

第7行展示了这种方式的用法. 而且从结果中也可以看到,拿到的就是外嵌类实例的引用.

.new
如果想在外嵌类中直接创建某个内部类对象. 还是使用new表达式.但是有点小区别.

1.public class DotNew {
2. public class Inner{
3. String str = "I am a inner class";
4. public String getStr(){
5. return str;
6. }
7. }
8. public static void main(String[] args) {
9. DotNew dn = new DotNew();
10. DotNew.Inner dni = dn.new Inner();
11. System.out.println(dni.getStr());
12. }
13.}

正常创建一个类实例的时候是ClassName instanceName = new ClassConstructor(). 创建内部类实例的时候其实也是这个样子. 只不过要知道如何找到确切的类名和构造器.第10行展示了如何直接创建内部类实例.

内部类和向上转型

之前说内部类可以很好的隐藏名称空间和代码组织. 但是之前的做法不够地道. 这里给出一个更地道的实现.

1.class Parcel4{
2. private class PContents implements Contents{
3. private int i = 11;
4. @Override
5. public int value() {
6. return i;
7. }
8. }
9. private class PDestination implements Destination{
10. private String label;
11. public PDestination(String whereTo) {
12. label=whereTo;
13. }
14. @Override
15. public String readLabel() {
16. return label;
17. }
18. }
19. public Destination destination(String dest){
20. return new PDestination(dest);
21. }
22. public Contents content(){
23. return new PContents();
24. }
25.}
26.public class TestParcel4 {
27.
28. public static void main(String[] args) {
29. Parcel4 p = new Parcel4();
30. Contents c = p.content();
31. Destination d = p.destination("USA");
32. System.out.println(c.value());
33. System.out.println(d.readLabel());
34. //can not access Parcel4's private member
35. //Parcel4.PContents pc = p.new PContents();
36. }
37.}

这里把内部类声明为private,然后给出两个public的方法来获取内部类. 但是,这个public的方法返回的是其接口类型.(也就是做了个向上转型) 这时实现某个接口的内部类对外是完全不可见的,甚至不知道其具体的类型. 这种隐藏就更彻底了. private的内部类给类的设计者提供了一种更强大的隐藏方式. 完全隐藏实现细节,甚至可以完全阻止任何依赖于类型的编码. 需要注意的一点是,这种方式,内部类做了向上转型,所以内部类中实现的扩展的接口是没有意义的.

局部内部类

前面所说的内部类都是以类似成员变量的方式定义的. 也就是直接定义在外嵌类的里面. 内部类还可以定义在方法内或者任意的作用域内,也就是传说中的局部内部类. (最蛋疼的一种内部类,没有之一)

1.public class Parcel5 {
2. public Destination destination(String s){
3. class PDestination implements Destination{
4. String label;
5. private PDestination(String whereTo) {
6. label = whereTo;
7. }
8. @Override
9. public String readLabel() {
10. return label;
11. }
12. }
13. return new PDestination(s);
14. }
15. public static void main(String[] args) {
16. Parcel5 p = new Parcel5();
17. Destination d = p.destination("USA");
18. System.out.println(d.readLabel());
19. }
20.}

在任意的作用于内嵌入内部类

1.public class Parcel6 {
2. private void internalTracking(boolean b) {
3. if(b) {
4. class TrackingSlip {
5. private String id;
6. TrackingSlip(String s) {
7. id = s;
8. }
9. String getSlip() { return id; }
10. }
11. TrackingSlip ts = new TrackingSlip("slip");
12. String s = ts.getSlip();
13. }
14. // Can't use it here! Out of scope:
15. //! TrackingSlip ts = new TrackingSlip("x");
16. }
17. public void track() { internalTracking(true); }
18. public static void main(String[] args) {
19. Parcel6 p = new Parcel6();
20. p.track();
21. }
22. } ///:~

这一节是最头疼的一节,看上去很好理解有不知道哪里会用到它. 埃大爷说了,定义这种内部类有两个理由:

  1. 实现了某个类型的接口,于是可以创建并返回对其的引用(啥意思没看懂…)
  2. 需要解决一个复杂的问题,想创建一个类来辅助解决,但是又不希望这个类是公共可用的. (那这个问题得多复杂,既然复杂度那么高,是不是可以再拆分一下问题模型呢…)

匿名内部类

局部内部类貌似使用的不是很多,而且很多时候是可以被匿名内部类代替的.

匿名内部类的定义和初始化

按照之前的设定,想使用一个内部类是这样写的.

1.public class Parcel7b {
2.
3. class MyContents implements Contents {
4. private int i = 11;
5. public int value() {
6. return i;
7. }
8. }
9. public Contents contents(){
10. return new MyContents();
11. }
12. public static void main(String[] args) {
13. Parcel7b p = new Parcel7b();
14. Contents c = p.contents();
15. System.out.println(c.value());
16. }
17.
18.}

我们的主要目的是使用contents方法来拿到一个关于实现了Contents接口的内部类的实例的引用. java对于这种场景给出了一种新的语法,让我们可以通过更少的代码实现这一功能. 像下面这样.

1.public class Parcel7 {
2. public Contents contents(){
3.
4. //anonymous innner class
5. return new Contents() {
6. int i = 11;
7. @Override
8. public int value() {
9. // TODO Auto-generated method stub
10. return i;
11. }
12. };
13. }
14.
15. public static void main(String[] args) {
16. Parcel7 p = new Parcel7();
17. Contents c = p.contents();
18. System.out.println(c.value());
19. }
20.}

当然也可以使用带参数的构造器. 这时候直接使用基类的构造器就可以了.

1.public class Parcel8 {
2. public Wrapping wrapping(int x) {
3. // Base constructor call:
4. return new Wrapping(x) { // Pass constructor argument.
5. public int value() {
6. return super.value() * 47;
7. }
8. }; // Semicolon required
9. }
10. public static void main(String[] args) {
11. Parcel8 p = new Parcel8();
12. Wrapping w = p.wrapping(10);
13. }
14. } ///:~
15.//=======================================================================
16.public class Wrapping {
17. private int i;
18. public Wrapping(int x) { i = x; }
19. public int value() { return i; }
20. } ///:~

在匿名类中初始化参数

1.
2.public class Parcel9 {
3. private int i =10;
4. public Destination destination(String dest){
5. //public Destination destination(final String dest){
6. return new Destination() {
7. private String label = dest;
8. int j =i;
9. @Override
10. public String readLabel() {
11. // TODO Auto-generated method stub
12. System.out.println(j);
13. return label;
14. }
15. };
16. }
17. public static void main(String[] args) {
18. Parcel9 p = new Parcel9();
19. Destination d = p.destination("USA");
20.
21. System.out.println(d.readLabel());
22. }
23.}

匿名内部类还是一个内部类,内部类的特性它还是有的,比如可以无条件的访问外嵌类的成员. 但是要求形参引用类型必须是final,但是我试了一下,1.6版本中还有这个限制,在1.8中没有这个限制了. 这种方法简单的给一个字段赋值是可以的,但是如果想要完成一些构造器的行为,怎么办呢?

首先匿名内部类中是不可能有命名构造器的. 但是通过使用实例初始化器可以达到类似的效果.

1.abstract class Base{
2. public Base(int i){
3. System.out.println("Base constructor, i= "+i);
4. }
5. public abstract void f();
6.}
7.
8.public class AnonymousConstructor {
9. public static Base getBase(int i){
10. return new Base(i){
11. {System.out.println("inside instance initializer");}
12. public void f(){
13. System.out.println("in anonymous f()");
14. }
15. };
16. }
17. public static void main(String[] args) {
18. Base base = AnonymousConstructor.getBase(47);
19. base.f();
20. }
21.}

再比如:

1.public class Parcel10 {
2. public Destination destination(String dest,float price){
3. return new Destination() {
4. private float cost;
5. {
6. cost = Math.round(price);
7. if(cost>100){
8. System.out.println("over budget");
9. }
10.
11. }
12. private String label = dest;
13. @Override
14. public String readLabel() {
15. // TODO Auto-generated method stub
16. return label;
17. }
18. };
19. }
20. public static void main(String[] args) {
21. Parcel10 p = new Parcel10();
22. p.destination("USA", 101.123f);
23. }
24.}

上面代码5-9行是一个实例初始化器,它的作用在匿名内部类中就相当于一个构造器. 但是匿名内部类中是有限制的,比如,构造器不能重载,而且只能继承一个类,或者实现一个接口,并不能两者兼备.

使用匿名内部类来实现工厂模式

1.interface Service {
2. void method1();
3.
4. void method2();
5.}
6.
7.interface ServiceFactory {
8. Service getService();
9.}
10.
11.class Implementation1 implements Service {
12. private Implementation1() {
13. }
14.
15. @Override
16. public void method1() {
17. System.out.println("I1m1");
18. }
19.
20. @Override
21. public void method2() {
22. System.out.println("I1m2");
23. }
24.
25. public static ServiceFactory factory() {
26. return new ServiceFactory() {
27. @Override
28. public Service getService() {
29. // TODO Auto-generated method stub
30. return new Implementation1();
31. }
32. };
33. }
34.
35.}
36.
37.class Implementation2 implements Service {
38. private Implementation2() {
39. }
40.
41. @Override
42. public void method1() {
43. System.out.println("I2m1");
44. }
45.
46. @Override
47. public void method2() {
48. System.out.println("I2m2");
49. }
50.
51. public static ServiceFactory factory() {
52. return new ServiceFactory() {
53. @Override
54. public Service getService() {
55. // TODO Auto-generated method stub
56. return new Implementation2();
57. }
58. };
59. }
60.
61.}
62.
63.public class Factories {
64. public static void serviceConsumer(ServiceFactory fact) {
65. Service s = fact.getService();
66. s.method1();
67. s.method2();
68. }
69. public static void main(String[] args) {
70. serviceConsumer(Implementation1.factory());
71. serviceConsumer(Implementation2.factory());
72. }
73.}

上一章中说到了工厂模式,那个时候还需要创建一个Implementation1Factory的类,有了匿名内部类之后,这个就不需要创建了. 像25行,51行使用了匿名内部类.

嵌套类

如果不需要内部类对象与外围类对象之间有联系,那可以将内部类对象声明为static. 这样就有了嵌套类,也叫静态内部类. 非static修饰的内部类都会保有外嵌类的this指针. 但是嵌套内部类没有这个指针. 所以一个内嵌类看上去有点类似一个static方法. 埃大爷说:

  1. 要创建嵌套类的对象,不需要其外围类的对象.
  2. 不能从嵌套类对象中访问非静态的外围类的对象.

嵌套类与普通内部类还有一个区别,也是之前没有说道的一个限制. 普通的内部类中不能含有static成员.也就是所有static成员必须放在外嵌类中生命. 但是嵌套类中是可以声明static成员的.

1.public class Parcel11 {
2. //nested classes
3. private static class ParcelContents implements Contents{
4. private int i =11;
5. @Override
6. public int value() {
7. return i;
8. }
9. }
10.
11. protected static class ParcelDestination implements Destination{
12. String label;
13. private ParcelDestination(String whereTo) {
14. label = whereTo;
15. }
16. @Override
17. public String readLabel() {
18.
19. return label;
20. }
21. public static void f(){
22. System.out.println("f()");
23. }
24. //we can define static members in a nested class
25. static int x =10;
26. static class AnotherLevel{
27. public static void f(){
28. System.out.println("f()");
29. }
30. static int x =10;
31. }
32. }
33. public static Destination destination(String s){
34. return new ParcelDestination(s);
35. }
36. public static Contents contents(){
37. return new ParcelContents();
38. }
39.
40. public static void main(String[] args) {
41. Contents c = contents();
42. Destination d = destination("USA");
43. }
44.}

嵌套类大概就是这个样子,注意!!!

  • main方法中并没有创建外嵌类的实例,但是依旧可以创建内嵌类的实例.
  • 25,26行,在一个内嵌类的内部创建了static成员.

接口内部的类
我们来做一个不严谨的推导,所有类都可以创建内部类–>java支持内嵌类(说起来好别扭,感觉静态内部类听起来好听一点…)–>接口是一种特殊的类–>所以接口里也可以声明内嵌类.
首先,我们要明确一个原则,接口更多的是提供一种协议,他其中不应该出现任何实现相关的代码. 但是,嵌套类可以作为接口的一部分. 接口中所有成员都是隐式public static的. 既然都是static的,它可以是static的变量,也可以是static的方法,那么为什么不可以是static的内部类呢?

1.public interface ClassInInterface {
2. void howdy();
3. class Test implements ClassInInterface{
4. @Override
5. public void howdy() {
6. System.out.println("Howdy!");
7. }
8. public static void main(String[] args) {
9. new Test().howdy();
10. }
11. }
12.}

像这个样子. 埃大爷说如果想给一个接口的所有实现提供公共的代码,可以使用这种方式. 但是我实在是理解不了…

从多层嵌套类中访问外部类的成员
一句话,一个内部类被嵌套多少层都不重要,它都可以透明的访问所有外部类的成员.

1.class MNA{
2. private void f(){
3. System.out.println("111");
4. }
5. class A{
6. private void g(){
7. System.out.println("222");
8. }
9. public class B{
10. void h(){
11. g();
12. f();
13. }
14. }
15. }
16.}
17.public class MultiNestingAccess {
18. public static void main(String[] args) {
19. MNA mna = new MNA();
20. MNA.A mnaa = mna.new A();
21. MNA.A.B mnaab = mnaa.new B();
22. mnaab.h();
23. }
24.}

内部类的继承

内部类的构造器必须要连接到指向外部类的引用,也就是那个.new语法所以如果想要继承内部类的时候,这个指向外嵌类对象的隐式的引用必须被初始化,而在子类中不再存在可连接的默认对象. 这个时候就需要用一些特殊手段了. (这么复杂的情况已经完全超出我的认知范围了,请自行脑补看到这一节的时候,我的表情)

1.class WithInner{
2. class Inner{}
3.}
4.public class InheritInner extends WithInner.Inner{
5. //can not use
6. //public InheritInner() {}
7. public InheritInner(WithInner wi) {
8. wi.super();
9. }
10. public static void main(String[] args) {
11.
12. WithInner wi = new WithInner();
13. //must use outer class' reference to complete the construction
14. InheritInner ii = new InheritInner(wi);
15. }
16.}

注意7-9行的代码,继承一个内部类的时候,构造方法必须传递一个外部类的引用,并且使用super()语法进行初始化.

覆盖内部类

内部类有时候看上去更像是一个方法,那么有没有”重写”一说呢? 再推导一下,内部类看上去像一个成员方法,成员方法是可以被重写的,那么内部类在继承的时候,也是可以被覆盖的,是吗?! 码一发!

1.class Egg{
2. private Yolk y;
3. protected class Yolk{
4. public Yolk(){
5. System.out.println("Egg.Yolk()");
6. }
7. }
8. public Egg(){
9. System.out.println("New Egg()");
10. y=new Yolk();
11. }
12.}
13.public class BigEgg extends Egg{
14. public class Yolk{
15. public Yolk(){
16. System.out.println("BigEgg.Yolk()");
17. }
18. }
19. public static void main(String[] args) {
20. new BigEgg();
21. }
22.}/*output
23.New Egg()
24.Egg.Yolk()
25.*/

不好意思,没有被覆盖. 这是为啥呢?
这要从内部类编译的产物来说了. 一个包含内部类的类(埃大爷说的很学术范儿,叫编译单元,就是个.java的源文件)编译完之后会生成类似那么几个玩意儿. 内部类会是一个独立的class文件,但是命名上会以$来连接外嵌类和内部类. 对于匿名内部类,$后是一串数字.
Parcel1.class
Parcel1$Destination.class
Parcel1$Contents.class

也就是说,外嵌类和内部类编译之后其实是两个独立的实体,有着各自的命名空间. 它不存在被重写一说. 但是能不能达到一个重写的效果呢,不用犹豫,肯定能.

1.class Egg2{
2. private Yolk y=new Yolk();
3. protected class Yolk{
4. public Yolk(){
5. System.out.println("Egg2.Yolk()");
6. }
7. public void f(){
8. System.out.println("Egg2.Yolk.f()");
9. }
10. }
11. public Egg2(){
12. System.out.println("New Egg2()");
13. }
14. public void insertYolk(Yolk yy){
15. y=yy;
16. }
17. public void g(){
18. y.f();
19. }
20.
21.}
22.
23.public class BigEgg2 extends Egg2{
24. public class Yolk extends Egg2.Yolk{
25. public Yolk(){
26. System.out.println("BigEgg2.Yolk()");
27. }
28. public void f(){
29. System.out.println("BigEgg2.Yolk.f()");
30. }
31. }
32. public BigEgg2(){insertYolk(new Yolk());}
33. public static void main(String[] args) {
34. Egg2 e2=new BigEgg2();
35. e2.g();
36. }
37.}/*output
38.Egg2.Yolk()
39.New Egg2()
40.Egg2.Yolk()
41.BigEgg2.Yolk()
42.BigEgg2.Yolk.f()
43.*/

局部内部类和匿名内部类的区别

局部内部类不能有权限修饰符,因为它不是外嵌类的一部分. 但是它可以访问当前代码块的常量还有外嵌类的所有成员.

1.interface Counter{
2. int next();
3.}
4.public class LocalInnerClass {
5. private int count=0;
6.
7. Counter getCounter(String name){
8. class LocalCounter implements Counter{
9. public LocalCounter(){
10. System.out.println("LocalCounter()");
11. }
12. @Override
13. public int next(){
14. System.out.print(name+" ");
15. return count++;
16. }
17. }
18. return new LocalCounter();
19. }
20.
21. Counter getCounter2(String name){
22. return new Counter(){
23. {
24. System.out.println("Counter()");
25. }
26. @Override
27. public int next() {
28. System.out.print(name+" ");
29. return count++;
30. }
31. };
32. }
33. public static void main(String[] args) {
34. LocalInnerClass lic = new LocalInnerClass();
35. Counter c1 = lic.getCounter("local innner");
36. Counter c2 = lic.getCounter2("Anonymous inner");
37.
38. for (int i =0;i<5;i++){
39. System.out.println(c1.next());
40. }
41. for(int i =0;i<5;i++){
42. System.out.println(c2.next());
43. }
44. }
45.}/*output
46.LocalCounter()
47.Counter()
48.local innner 0
49.local innner 1
50.local innner 2
51.local innner 3
52.local innner 4
53.Anonymous inner 5
54.Anonymous inner 6
55.Anonymous inner 7
56.Anonymous inner 8
57.Anonymous inner 9
58.*/

从代码上看,这一段代码匿名内部类和局部内部类实现了相同的功能. 那么他们的区别在哪里?何时用匿名内部类何时用局部内部类? 如果我们需要用一个命名构造器,或者需要重载构造器的时候,使用局部内部类,因为匿名内部类只能完成实例的初始化.

%23TJI%u8BFB%u4E66%u7B14%u8BB013-%u5185%u90E8%u7C7B%0A@%28034.java%29%5Bjava%7Cjava%u7F16%u7A0B%u601D%u60F3%5D%0A%0A%5BTOC%5D%0A%0A%u6211%u6700%u6076%u5FC3%u7684%u4E24%u90E8%u5206%u5185%u5BB9%2C%u4E00%u4E2A%u53EBlambda%u8868%u8FBE%u5F0F%2C%u4E00%u4E2A%u53EB%u5185%u90E8%u7C7B.%20%u667A%u5546%u6709%u9650%2C%u4E00%u76F4%u7406%u89E3%u4E0D%u4E86...%0A%0A%u7B80%u5355%u6765%u8BF4%2C%u5C06%u4E00%u4E2A%u7C7B%u7684%u5B9A%u4E49%u653E%u5728%u53E6%u4E00%u4E2A%u7C7B%u7684%u5185%u90E8%u5C31%u662F%u5185%u90E8%u7C7B.%20%u5185%u90E8%u7C7B%u7684%u597D%u5904%u5728%u4E8E%2C%u5C06%u4E00%u4E9B%u903B%u8F91%u4E0A%u76F8%u5173%u7684%u7C7B%u7EC4%u7EC7%u5728%u4E00%u8D77%2C%u5E76%u63A7%u5236%u4F4D%u4E8E%u5185%u90E8%u7684%u7C7B%u7684%u53EF%u89C6%u6027.%u5185%u90E8%u7C7B%u53EF%u4EE5%u4E86%u89E3%u5916%u56F4%u7C7B%2C%u5E76%u4E0E%u4E4B%u901A%u4FE1.%20%u4F46%u662F%u5185%u90E8%u7C7B%u548C%u7EC4%u5408%u662F%u4E24%u4E2A%u6982%u5FF5%2C%u6BD4%u5982%u5C06%u4EBA%u7684%u5927%u817F%u5B9A%u4E49%u5728%u4EBA%u8FD9%u4E2A%u7C7B%u7684%u5185%u90E8%2C%u8FD9%u5C31%u662F%u4E00%u4E2A%u6BD4%u8F83%u5408%u7406%u7684%u8BBE%u5B9A.%u5982%u679C%u901A%u8FC7%u7EC4%u5408%2C%u4E5F%u53EF%u4EE5%u5B9E%u73B0%2C%u4F46%u662F%u603B%u662F%u6709%u90A3%u4E48%u4E00%u4E22%u4E22%u522B%u626D%u7684.%20%u56E0%u4E3A%u5927%u817F%u8131%u79BB%u4E86%u4EBA%2C%u51E0%u4E4E%u5C31%u6CA1%u7528%u4EFB%u4F55%u610F%u4E49%u4E86.%20%0A%0A%23%23%u521B%u5EFA%u5185%u90E8%u7C7B%0A%u6765%u5427%2C%u4E00%u7801%u89E3%u5343%u6101%0A%60%60%60java%0Apublic%20class%20Parcel1%20%7B%0A%09//a%20inner%20class%0A%09class%20Contents%7B%0A%09%09private%20int%20i%20%3D%2011%3B%0A%09%09public%20int%20value%28%29%7B%0A%09%09%09return%20i%3B%0A%09%09%7D%09%09%0A%09%7D%0A%09%0A%09//a%20inner%20class%0A%09class%20Destination%7B%0A%09%09private%20String%20label%3B%0A%09%09public%20Destination%28String%20whereTo%29%20%7B%0A%09%09%09label%3DwhereTo%3B%0A%09%09%7D%0A%09%09String%20readLabel%28%29%7B%0A%09%09%09return%20label%3B%0A%09%09%7D%0A%09%7D%0A%09%0A%09public%20void%20ship%28String%20dest%29%7B%0A%09%09Contents%20c%20%3D%20new%20Contents%28%29%3B%0A%09%09Destination%20d%20%3D%20new%20Destination%28dest%29%3B%0A%09%09System.out.println%28c.value%28%29%29%3B%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel1%20p%20%3D%20new%20Parcel1%28%29%3B%0A%09%09p.ship%28%22USA%22%29%3B%0A%09%09%0A%09%7D%0A%7D%0A%60%60%60%0A%u8FD9%u662F%u4E00%u4E2A%u975E%u5E38%u7B80%u5355%u7684%u5E94%u7528%2C%u5C31%u50CF%u4F7F%u7528%u5176%u4ED6%u666E%u901A%u7684%u7C7B%u4E00%u6837%u53BB%u4F7F%u7528%u5185%u90E8%u7C7B.%u66F4%u5178%u578B%u7684%u60C5%u51B5%2C%u5728%u5916%u90E8%u7C7B%u4E2D%u63D0%u4F9B%u4E00%u4E2A%u65B9%u6CD5%u8FD4%u56DE%u4E00%u4E2A%u6307%u5411%u5185%u90E8%u7C7B%u5B9E%u4F8B%u7684%u5F15%u7528.%20%0A%60%60%60java%0Apublic%20class%20Parcel2%20%7B%0A%09class%20Contents%20%7B%0A%09%09private%20int%20i%20%3D%2011%3B%0A%0A%09%09public%20int%20value%28%29%20%7B%0A%09%09%09return%20i%3B%0A%09%09%7D%0A%09%7D%0A%0A%09class%20Destination%20%7B%0A%09%09private%20String%20label%3B%0A%0A%09%09public%20Destination%28String%20whereTo%29%20%7B%0A%09%09%09label%20%3D%20whereTo%3B%0A%09%09%7D%0A%0A%09%09String%20readLabel%28%29%20%7B%0A%09%09%09return%20label%3B%0A%09%09%7D%0A%09%7D%0A%0A%09public%20Destination%20to%28String%20s%29%20%7B%0A%09%09return%20new%20Destination%28s%29%3B%0A%09%7D%0A%0A%09public%20Contents%20contents%28%29%20%7B%0A%09%09return%20new%20Contents%28%29%3B%0A%09%7D%0A%0A%09public%20void%20ship%28String%20dest%29%20%7B%0A%09%09Contents%20c%20%3D%20this.contents%28%29%3B%0A%09%09Destination%20d%20%3D%20this.to%28dest%29%3B%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%7D%0A%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel2%20p1%20%3D%20new%20Parcel2%28%29%3B%0A%09%09p1.ship%28%22USA%22%29%3B%0A%09%09%0A%09%09//%20define%20reference%20to%20inner%20classes%0A%09%09Parcel2%20p2%20%3D%20new%20Parcel2%28%29%3B%0A%09%09Parcel2.Contents%20c%20%3D%20p2.contents%28%29%3B%0A%09%09Parcel2.Destination%20d%20%3D%20p2.to%28%22UK%22%29%3B%0A%09%09System.out.println%28c.value%28%29%29%3B%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u6CE8%u610F%2C22%u884C%2C26%u884C%u5B9A%u4E49%u4E86%u4E24%u4E2A%u65B9%u6CD5%u7528%u6765%u8FD4%u56DE%u5185%u90E8%u7C7B%u7684%u5B9E%u4F8B.%20%u901A%u8FC7%u8FD9%u79CD%u65B9%u5F0F%u53EF%u4EE5%u66F4%u597D%u7684%u505A%u6821%u9A8C%u903B%u8F91.%20%u53E6%u5916%2C%u8FD8%u670942-43%u884C%2C%u5728%u5916%u90E8%u7C7B%u7684%u975E%u9759%u6001%u65B9%u6CD5%u4E4B%u5916%u7684%u5730%u65B9%u521B%u5EFA%u5185%u90E8%u7C7B%u5B9E%u4F8B%u7684%u65F6%u5019%2C%u9700%u8981%u7528%60OuterClassName.InnerClassName%60%u7684%u65B9%u5F0F%u5177%u4F53%u6307%u660E%u5BF9%u8C61%u7684%u7C7B%u578B.%20%0A%3E%u7A81%u7136%u60F3%u5230%u4E00%u4E2A%u95EE%u9898%2C%u5185%u90E8%u7C7B%u5728%u52A0%u8F7D%u8FC7%u7A0B%u4E2D%u662F%u4E2A%u4EC0%u4E48%u6837%u5B50%u7684%3F%0A%0A%23%23%u5185%u90E8%u7C7B%u548C%u5916%u90E8%u7C7B%u7684%u5173%u7CFB%0A%u4ECE%u524D%u9762%u4E00%u5C0F%u8282%2C%u6211%u4EEC%u53EF%u4EE5%u5F97%u77E5%u5185%u90E8%u7C7B%u7684%u4E00%u4E2A%u4F5C%u7528%3A%u5BF9%u5916%u9690%u85CF%u540D%u79F0%u7A7A%u95F4%u548C%u7EC4%u7EC7%u4EE3%u7801.%20%u8FD9%u4E00%u8282%u5520%u5520%u4E00%u4E2A%u66F4%u91CD%u8981%u7684%u7279%u6027%2C%u5185%u90E8%u7C7B%u53EF%u4EE5%u65E0%u6761%u4EF6%u7684%u8BBF%u95EE%u5916%u90E8%u5D4C%u5957%u7C7B%u7684%u6240%u6709%u6210%u5458.%u5185%u90E8%u7C7B%u7684%u53EF%u89C6%u8303%u56F4%u662F%u5B83%u7684%u76F4%u63A5%u5916%u5D4C%u7C7B%2C%u4E5F%u5C31%u662F%u8BF4%u5185%u90E8%u7C7B%u5176%u5B9E%u662F%u62E5%u6709%u4E24%u4E2Athis%u6307%u9488%2C%u4E00%u4E2A%u6307%u5411%u5B83%u81EA%u5DF1%u7684%u5B9E%u4F8B%u672C%u8EAB%2C%u53E6%u5916%u4E00%u4E2A%u6307%u5411%u5916%u5D4C%u7C7B%u7684%u5B9E%u4F8B.%20%0A%0A%60%60%60java%0Ainterface%20Selector%20%7B%0A%09//check%20if%20the%20sequence%20cursor%20goto%20end.%20%0A%09boolean%20end%28%29%3B%0A%09//return%20the%20object%20where%20the%20current%20cursor%20pointed%0A%09Object%20current%28%29%3B%0A%09//move%20the%20cursor%20to%20next%20element%20%0A%09void%20next%28%29%3B%0A%7D%0A%0Apublic%20class%20Sequence%20%7B%0A%09private%20Object%5B%5D%20items%3B%0A%09private%20int%20next%20%3D%200%3B%0A%09public%20Sequence%28int%20size%29%7B%0A%09%09items%20%3D%20new%20Object%5Bsize%5D%3B%0A%09%7D%0A%09public%20void%20add%28Object%20x%29%7B%0A%09%09if%28next%3Citems.length%29%7B%0A%09%09%09items%5Bnext++%5D%20%3D%20x%3B%0A%09%09%7D%0A%09%7D%0A%09private%20class%20SequenceSelector%20implements%20Selector%7B%0A%09%09private%20int%20i%20%3D0%3B%0A%09%09public%20boolean%20end%28%29%7B%0A%09%09%09return%20i%20%3D%3D%20items.length%3B%0A%09%09%7D%0A%09%09public%20Object%20current%28%29%7B%0A%09%09%09return%20items%5Bi%5D%3B%0A%09%09%7D%0A%09%09public%20void%20next%28%29%7B%0A%09%09%09if%28i%3Citems.length%29%20i++%3B%0A%09%09%7D%0A%09%7D%0A%09%0A%09public%20Selector%20selector%28%29%7B%0A%09%09return%20new%20SequenceSelector%28%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Sequence%20sequence%20%3D%20new%20Sequence%2810%29%3B%0A%09%09for%28int%20i%20%3D0%3Bi%3C10%3Bi++%29%7B%0A%09%09%09sequence.add%28Integer.toString%28i%29%29%3B%0A%09%09%7D%0A%09%09Selector%20selector%20%3D%20sequence.selector%28%29%3B%0A%09%09while%28%21selector.end%28%29%29%7B%0A%09%09%09System.out.print%28selector.current%28%29+%22%20%22%29%3B%0A%09%09%09selector.next%28%29%3B%0A%09%09%7D%0A%09%7D%0A%7D%0A%60%60%60%0A%0A%u8FD9%u662F%u4E00%u4E2A%u5178%u578B%u7684%u8FED%u4EE3%u5668%u539F%u578B%2C%u6CE8%u610F%2C24%2C27%2C30%u884C%u5747%u8BBF%u95EE%u4E86%u5916%u5D4C%u7C7B%u7684%u6210%u5458%20items.%20%u8FD9%u4E2A%u662F%u53EF%u4EE5%u7684.%20%u8BF4%u660E%u5185%u90E8%u7C7B%u53EF%u4EE5%u8BBF%u95EE%u5916%u5D4C%u7C7B%u6210%u5458.%u53EF%u4EE5%u8BF4%u5185%u90E8%u7C7B%u4F7F%u7528%u5916%u5D4C%u7C7B%u7684%u6210%u5458%u65F6%2C%u5C31%u50CF%u8BBF%u95EE%u5B83%u81EA%u5DF1%u7684%u6210%u5458%u4E00%u6837.%20%u4E5F%u5C31%u662F%u8BF4%u5185%u90E8%u7C7B%20%u81EA%u52A8%u62E5%u6709%u5176%u5916%u5D4C%u7C7B%u6240%u6709%u6210%u5458%u7684%u8BBF%u95EE%u6743%2C%u8FD9%u662F%u600E%u4E48%u505A%u5230%u7684%u5462%3F%u57C3%u5927%u7237%u8BF4%2C%u5F53%u5916%u5D4C%u7C7B%u5B9E%u4F8B%u521B%u5EFA%u5185%u90E8%u7C7B%u5B9E%u4F8B%u7684%u65F6%u5019%2C%u6B64%u5185%u90E8%u7C7B%u5BF9%u8C61%u5FC5%u5B9A%u4F1A%u6355%u83B7%u4E00%u4E2A%u6307%u5411%u5916%u5D4C%u7C7B%u5BF9%u8C61%u7684%u5F15%u7528.%20%u5F53%u6211%u4EEC%u8BBF%u95EE%u5916%u5D4C%u7C7B%u7684%u6210%u5458%u7684%u65F6%u5019%2C%u5C31%u662F%u4F7F%u7528%u90A3%u4E2A%u5F15%u7528%u6765%u9009%u62E9%u5916%u5D4C%u7C7B%u7684%u6210%u5458.%20%u8FD9%u4E00%u5207%u90FD%u662F%u9690%u5F0F%u5B8C%u6210%u7684%2C%u6211%u4EEC%u4E0D%u9700%u8981%u64CD%u5FC3.%20%u5176%u5B9E%u4E5F%u5C31%u662F%u5F00%u5934%u7684%u65F6%u5019%u8BF4%u7684%2C%u5185%u90E8%u7C7B%u4F1A%u62E5%u6709%u4E24%u4E2Athis%u6307%u9488.%20%0A%0A%u8FD8%u6709%u4E00%u4E2A%u5730%u65B9%u9700%u8981%u6CE8%u610F%2C%u7B2C21%u884C%2C%u5185%u90E8%u7C7B%u4F7F%u7528%u7684private%u4FEE%u9970%u7B26.%20%u8FD9%u4E2A%u8868%u793A%u5728%u5916%u5D4C%u7C7B%u4E4B%u5916%2C%u5185%u90E8%u7C7B%u4E0D%u53EF%u89C1.%20%u4E5F%u5C31%u662F%u8BF4%u5185%u90E8%u7C7B%u662F%u53EF%u4EE5%u4F7F%u7528private%u4FEE%u9970%u7684.%20%u4F46%u662F%u4E0D%u8981%u5FD8%u4E86%2C%u666E%u901A%u7684%u7C7B%u662F%u4E0D%u53EF%u4EE5%u4F7F%u7528private%u4FEE%u9970.%20%0A%0A%23%23.this%u548C.new%0A%0A**.this**%0A%0A%u5982%u679C%u9700%u8981%u5728%u5185%u90E8%u7C7B%u4E2D%u83B7%u53D6%u5916%u5D4C%u7C7B%u7684%u5F15%u7528%2C%u53EF%u4EE5%u4F7F%u7528%60OuterClassName.this%60%u7684%u65B9%u5F0F%u6765%u83B7%u53D6.%20%0A%60%60%60java%0Apublic%20class%20DotThis%20%7B%0A%09void%20f%28%29%7B%0A%09%09System.out.println%28%22DoThis.f%28%29%22%29%3B%0A%09%7D%0A%09public%20class%20Inner%7B%0A%09%09public%20DotThis%20outer%28%29%7B%0A%09%09%09return%20DotThis.this%3B%0A%09%09%7D%0A%09%7D%0A%09public%20Inner%20inner%28%29%7B%0A%09%09return%20new%20Inner%28%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09DotThis%20dt%20%3D%20new%20DotThis%28%29%3B%0A%09%09DotThis.Inner%20dti%20%3D%20dt.inner%28%29%3B%0A%09%09dti.outer%28%29.f%28%29%3B%0A%09%09%0A%09%09System.out.println%28dt%3D%3Ddti.outer%28%29%29%3B%0A%09%7D%0A%7D/*output%3A%0ADoThis.f%28%29%0Atrue%0A*/%0A%60%60%60%0A%u7B2C7%u884C%u5C55%u793A%u4E86%u8FD9%u79CD%u65B9%u5F0F%u7684%u7528%u6CD5.%20%u800C%u4E14%u4ECE%u7ED3%u679C%u4E2D%u4E5F%u53EF%u4EE5%u770B%u5230%2C%u62FF%u5230%u7684%u5C31%u662F%u5916%u5D4C%u7C7B%u5B9E%u4F8B%u7684%u5F15%u7528.%20%0A%0A**.new**%0A%u5982%u679C%u60F3%u5728%u5916%u5D4C%u7C7B%u4E2D%u76F4%u63A5%u521B%u5EFA%u67D0%u4E2A%u5185%u90E8%u7C7B%u5BF9%u8C61.%20%u8FD8%u662F%u4F7F%u7528new%u8868%u8FBE%u5F0F.%u4F46%u662F%u6709%u70B9%u5C0F%u533A%u522B.%20%0A%60%60%60java%0Apublic%20class%20DotNew%20%7B%0A%09public%20class%20Inner%7B%0A%09%09String%20str%20%3D%20%22I%20am%20a%20inner%20class%22%3B%0A%09%09public%20String%20getStr%28%29%7B%0A%09%09%09return%20str%3B%0A%09%09%7D%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09DotNew%20dn%20%3D%20new%20DotNew%28%29%3B%0A%09%09DotNew.Inner%20dni%20%3D%20dn.new%20Inner%28%29%3B%0A%09%09System.out.println%28dni.getStr%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u6B63%u5E38%u521B%u5EFA%u4E00%u4E2A%u7C7B%u5B9E%u4F8B%u7684%u65F6%u5019%u662F%60ClassName%20instanceName%20%3D%20new%20ClassConstructor%28%29%60.%20%u521B%u5EFA%u5185%u90E8%u7C7B%u5B9E%u4F8B%u7684%u65F6%u5019%u5176%u5B9E%u4E5F%u662F%u8FD9%u4E2A%u6837%u5B50.%20%u53EA%u4E0D%u8FC7%u8981%u77E5%u9053%u5982%u4F55%u627E%u5230%u786E%u5207%u7684%u7C7B%u540D%u548C%u6784%u9020%u5668.%u7B2C10%u884C%u5C55%u793A%u4E86%u5982%u4F55%u76F4%u63A5%u521B%u5EFA%u5185%u90E8%u7C7B%u5B9E%u4F8B.%20%0A%0A%23%23%u5185%u90E8%u7C7B%u548C%u5411%u4E0A%u8F6C%u578B%0A%u4E4B%u524D%u8BF4%u5185%u90E8%u7C7B%u53EF%u4EE5%u5F88%u597D%u7684%u9690%u85CF%u540D%u79F0%u7A7A%u95F4%u548C%u4EE3%u7801%u7EC4%u7EC7.%20%u4F46%u662F%u4E4B%u524D%u7684%u505A%u6CD5%u4E0D%u591F%u5730%u9053.%20%u8FD9%u91CC%u7ED9%u51FA%u4E00%u4E2A%u66F4%u5730%u9053%u7684%u5B9E%u73B0.%0A%0A%60%60%60java%0Aclass%20Parcel4%7B%0A%09private%20class%20PContents%20implements%20Contents%7B%0A%09%09private%20int%20i%20%3D%2011%3B%0A%09%09@Override%0A%09%09public%20int%20value%28%29%20%7B%0A%09%09%09return%20i%3B%0A%09%09%7D%0A%09%7D%0A%09private%20class%20PDestination%20implements%20Destination%7B%0A%09%09private%20String%20label%3B%0A%09%09public%20%20PDestination%28String%20whereTo%29%20%7B%0A%09%09%09label%3DwhereTo%3B%0A%09%09%7D%0A%09%09@Override%0A%09%09public%20String%20readLabel%28%29%20%7B%0A%09%09%09return%20label%3B%0A%09%09%7D%0A%09%7D%0A%09public%20Destination%20destination%28String%20dest%29%7B%0A%09%09return%20new%20PDestination%28dest%29%3B%0A%09%7D%0A%09public%20Contents%20content%28%29%7B%0A%09%09return%20new%20PContents%28%29%3B%0A%09%7D%0A%7D%0Apublic%20class%20TestParcel4%20%7B%0A%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel4%20p%20%3D%20new%20Parcel4%28%29%3B%0A%09%09Contents%20c%20%3D%20p.content%28%29%3B%0A%09%09Destination%20d%20%3D%20p.destination%28%22USA%22%29%3B%0A%09%09System.out.println%28c.value%28%29%29%3B%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%09//can%20not%20access%20Parcel4%27s%20private%20member%0A%09%09//Parcel4.PContents%20pc%20%3D%20p.new%20PContents%28%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u8FD9%u91CC%u628A%u5185%u90E8%u7C7B%u58F0%u660E%u4E3Aprivate%2C%u7136%u540E%u7ED9%u51FA%u4E24%u4E2Apublic%u7684%u65B9%u6CD5%u6765%u83B7%u53D6%u5185%u90E8%u7C7B.%20%u4F46%u662F%2C%u8FD9%u4E2Apublic%u7684%u65B9%u6CD5%u8FD4%u56DE%u7684%u662F%u5176%u63A5%u53E3%u7C7B%u578B.%28%u4E5F%u5C31%u662F%u505A%u4E86%u4E2A%u5411%u4E0A%u8F6C%u578B%29%20%u8FD9%u65F6%u5B9E%u73B0%u67D0%u4E2A%u63A5%u53E3%u7684%u5185%u90E8%u7C7B%u5BF9%u5916%u662F%u5B8C%u5168%u4E0D%u53EF%u89C1%u7684%2C%u751A%u81F3%u4E0D%u77E5%u9053%u5176%u5177%u4F53%u7684%u7C7B%u578B.%20%u8FD9%u79CD%u9690%u85CF%u5C31%u66F4%u5F7B%u5E95%u4E86.%20private%u7684%u5185%u90E8%u7C7B%u7ED9%u7C7B%u7684%u8BBE%u8BA1%u8005%u63D0%u4F9B%u4E86%u4E00%u79CD%u66F4%u5F3A%u5927%u7684%u9690%u85CF%u65B9%u5F0F.%20%u5B8C%u5168%u9690%u85CF%u5B9E%u73B0%u7EC6%u8282%2C%u751A%u81F3%u53EF%u4EE5%u5B8C%u5168%u963B%u6B62%u4EFB%u4F55%u4F9D%u8D56%u4E8E%u7C7B%u578B%u7684%u7F16%u7801.%20%u9700%u8981%u6CE8%u610F%u7684%u4E00%u70B9%u662F%2C%u8FD9%u79CD%u65B9%u5F0F%2C%u5185%u90E8%u7C7B%u505A%u4E86%u5411%u4E0A%u8F6C%u578B%2C%u6240%u4EE5%u5185%u90E8%u7C7B%u4E2D%u5B9E%u73B0%u7684%u6269%u5C55%u7684%u63A5%u53E3%u662F%u6CA1%u6709%u610F%u4E49%u7684.%20%0A%0A%23%23%u5C40%u90E8%u5185%u90E8%u7C7B%0A%u524D%u9762%u6240%u8BF4%u7684%u5185%u90E8%u7C7B%u90FD%u662F%u4EE5%u7C7B%u4F3C%u6210%u5458%u53D8%u91CF%u7684%u65B9%u5F0F%u5B9A%u4E49%u7684.%20%u4E5F%u5C31%u662F%u76F4%u63A5%u5B9A%u4E49%u5728%u5916%u5D4C%u7C7B%u7684%u91CC%u9762.%20%u5185%u90E8%u7C7B%u8FD8%u53EF%u4EE5%u5B9A%u4E49%u5728%u65B9%u6CD5%u5185%u6216%u8005%u4EFB%u610F%u7684%u4F5C%u7528%u57DF%u5185%2C%u4E5F%u5C31%u662F%u4F20%u8BF4%u4E2D%u7684%u5C40%u90E8%u5185%u90E8%u7C7B.%20%28%u6700%u86CB%u75BC%u7684%u4E00%u79CD%u5185%u90E8%u7C7B%2C%u6CA1%u6709%u4E4B%u4E00%29%0A%60%60%60java%0Apublic%20class%20Parcel5%20%7B%0A%09public%20Destination%20destination%28String%20s%29%7B%0A%09%09class%20PDestination%20implements%20Destination%7B%0A%09%09%09String%20label%3B%0A%09%09%09private%20PDestination%28String%20whereTo%29%20%7B%0A%09%09%09%09label%20%3D%20whereTo%3B%0A%09%09%09%7D%0A%09%09%09@Override%0A%09%09%09public%20String%20readLabel%28%29%20%7B%0A%09%09%09%09return%20label%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return%20new%20PDestination%28s%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel5%20p%20%3D%20new%20Parcel5%28%29%3B%0A%09%09Destination%20d%20%3D%20p.destination%28%22USA%22%29%3B%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A**%u5728%u4EFB%u610F%u7684%u4F5C%u7528%u4E8E%u5185%u5D4C%u5165%u5185%u90E8%u7C7B**%0A%60%60%60java%0Apublic%20class%20Parcel6%20%7B%0A%09%20%20private%20void%20internalTracking%28boolean%20b%29%20%7B%0A%09%20%20%20%20if%28b%29%20%7B%0A%09%20%20%20%20%20%20class%20TrackingSlip%20%7B%0A%09%20%20%20%20%20%20%20%20private%20String%20id%3B%0A%09%20%20%20%20%20%20%20%20TrackingSlip%28String%20s%29%20%7B%0A%09%20%20%20%20%20%20%20%20%20%20id%20%3D%20s%3B%0A%09%20%20%20%20%20%20%20%20%7D%0A%09%20%20%20%20%20%20%20%20String%20getSlip%28%29%20%7B%20return%20id%3B%20%7D%0A%09%20%20%20%20%20%20%7D%0A%09%20%20%20%20%20%20TrackingSlip%20ts%20%3D%20new%20TrackingSlip%28%22slip%22%29%3B%0A%09%20%20%20%20%20%20String%20s%20%3D%20ts.getSlip%28%29%3B%0A%09%20%20%20%20%7D%0A%09%20%20%20%20//%20Can%27t%20use%20it%20here%21%20Out%20of%20scope%3A%0A%09%20%20%20%20//%21%20TrackingSlip%20ts%20%3D%20new%20TrackingSlip%28%22x%22%29%3B%0A%09%20%20%7D%09%0A%09%20%20public%20void%20track%28%29%20%7B%20internalTracking%28true%29%3B%20%7D%0A%09%20%20public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%20%20%20%20Parcel6%20p%20%3D%20new%20Parcel6%28%29%3B%0A%09%20%20%20%20p.track%28%29%3B%0A%09%20%20%7D%0A%09%7D%20///%3A%7E%0A%60%60%60%0A%u8FD9%u4E00%u8282%u662F%u6700%u5934%u75BC%u7684%u4E00%u8282%2C%u770B%u4E0A%u53BB%u5F88%u597D%u7406%u89E3%u6709%u4E0D%u77E5%u9053%u54EA%u91CC%u4F1A%u7528%u5230%u5B83.%20%u57C3%u5927%u7237%u8BF4%u4E86%2C%u5B9A%u4E49%u8FD9%u79CD%u5185%u90E8%u7C7B%u6709%u4E24%u4E2A%u7406%u7531%3A%0A1.%20%u5B9E%u73B0%u4E86%u67D0%u4E2A%u7C7B%u578B%u7684%u63A5%u53E3%2C%u4E8E%u662F%u53EF%u4EE5%u521B%u5EFA%u5E76%u8FD4%u56DE%u5BF9%u5176%u7684%u5F15%u7528%28%u5565%u610F%u601D%u6CA1%u770B%u61C2...%29%0A2.%20%u9700%u8981%u89E3%u51B3%u4E00%u4E2A%u590D%u6742%u7684%u95EE%u9898%2C%u60F3%u521B%u5EFA%u4E00%u4E2A%u7C7B%u6765%u8F85%u52A9%u89E3%u51B3%2C%u4F46%u662F%u53C8%u4E0D%u5E0C%u671B%u8FD9%u4E2A%u7C7B%u662F%u516C%u5171%u53EF%u7528%u7684.%20%28%u90A3%u8FD9%u4E2A%u95EE%u9898%u5F97%u591A%u590D%u6742%2C%u65E2%u7136%u590D%u6742%u5EA6%u90A3%u4E48%u9AD8%2C%u662F%u4E0D%u662F%u53EF%u4EE5%u518D%u62C6%u5206%u4E00%u4E0B%u95EE%u9898%u6A21%u578B%u5462...%29%0A%0A%23%23%u533F%u540D%u5185%u90E8%u7C7B%0A%u5C40%u90E8%u5185%u90E8%u7C7B%u8C8C%u4F3C%u4F7F%u7528%u7684%u4E0D%u662F%u5F88%u591A%2C%u800C%u4E14%u5F88%u591A%u65F6%u5019%u662F%u53EF%u4EE5%u88AB%u533F%u540D%u5185%u90E8%u7C7B%u4EE3%u66FF%u7684.%20%0A%23%23%23%u533F%u540D%u5185%u90E8%u7C7B%u7684%u5B9A%u4E49%u548C%u521D%u59CB%u5316%0A%u6309%u7167%u4E4B%u524D%u7684%u8BBE%u5B9A%2C%u60F3%u4F7F%u7528%u4E00%u4E2A%u5185%u90E8%u7C7B%u662F%u8FD9%u6837%u5199%u7684.%20%0A%60%60%60java%0Apublic%20class%20Parcel7b%20%7B%0A%0A%09class%20MyContents%20implements%20Contents%20%7B%0A%09%09private%20int%20i%20%3D%2011%3B%0A%09%09public%20int%20value%28%29%20%7B%0A%09%09%09return%20i%3B%0A%09%09%7D%0A%09%7D%0A%09public%20Contents%20contents%28%29%7B%0A%09%09return%20new%20MyContents%28%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel7b%20p%20%3D%20new%20Parcel7b%28%29%3B%0A%09%09Contents%20c%20%3D%20p.contents%28%29%3B%0A%09%09System.out.println%28c.value%28%29%29%3B%0A%09%7D%0A%0A%7D%0A%60%60%60%0A%u6211%u4EEC%u7684%u4E3B%u8981%u76EE%u7684%u662F%u4F7F%u7528contents%u65B9%u6CD5%u6765%u62FF%u5230%u4E00%u4E2A%u5173%u4E8E%u5B9E%u73B0%u4E86Contents%u63A5%u53E3%u7684%u5185%u90E8%u7C7B%u7684%u5B9E%u4F8B%u7684%u5F15%u7528.%20java%u5BF9%u4E8E%u8FD9%u79CD%u573A%u666F%u7ED9%u51FA%u4E86%u4E00%u79CD%u65B0%u7684%u8BED%u6CD5%2C%u8BA9%u6211%u4EEC%u53EF%u4EE5%u901A%u8FC7%u66F4%u5C11%u7684%u4EE3%u7801%u5B9E%u73B0%u8FD9%u4E00%u529F%u80FD.%20%u50CF%u4E0B%u9762%u8FD9%u6837.%0A%60%60%60java%0Apublic%20class%20Parcel7%20%7B%0A%09public%20Contents%20contents%28%29%7B%0A%09%09%0A%09%09//anonymous%20innner%20class%0A%09%09return%20new%20Contents%28%29%20%7B%0A%09%09%09int%20i%20%3D%2011%3B%0A%09%09%09@Override%0A%09%09%09public%20int%20value%28%29%20%7B%0A%09%09%09%09//%20TODO%20Auto-generated%20method%20stub%0A%09%09%09%09return%20i%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%09%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel7%20p%20%3D%20new%20Parcel7%28%29%3B%0A%09%09Contents%20c%20%3D%20p.contents%28%29%3B%0A%09%09System.out.println%28c.value%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u5F53%u7136%u4E5F%u53EF%u4EE5%u4F7F%u7528%u5E26%u53C2%u6570%u7684%u6784%u9020%u5668.%20%u8FD9%u65F6%u5019%u76F4%u63A5%u4F7F%u7528%u57FA%u7C7B%u7684%u6784%u9020%u5668%u5C31%u53EF%u4EE5%u4E86.%20%0A%60%60%60java%0Apublic%20class%20Parcel8%20%7B%0A%09%20%20public%20Wrapping%20wrapping%28int%20x%29%20%7B%0A%09%20%20%20%20//%20Base%20constructor%20call%3A%0A%09%20%20%20%20return%20new%20Wrapping%28x%29%20%7B%20//%20Pass%20constructor%20argument.%0A%09%20%20%20%20%20%20public%20int%20value%28%29%20%7B%0A%09%20%20%20%20%20%20%20%20return%20super.value%28%29%20*%2047%3B%0A%09%20%20%20%20%20%20%7D%0A%09%20%20%20%20%7D%3B%20//%20Semicolon%20required%0A%09%20%20%7D%0A%09%20%20public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%20%20%20%20Parcel8%20p%20%3D%20new%20Parcel8%28%29%3B%0A%09%20%20%20%20Wrapping%20w%20%3D%20p.wrapping%2810%29%3B%0A%09%20%20%7D%0A%09%7D%20///%3A%7E%0A//%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0Apublic%20class%20Wrapping%20%7B%0A%09%20%20private%20int%20i%3B%0A%09%20%20public%20Wrapping%28int%20x%29%20%7B%20i%20%3D%20x%3B%20%7D%0A%09%20%20public%20int%20value%28%29%20%7B%20return%20i%3B%20%7D%0A%09%7D%20///%3A%7E%0A%60%60%60%0A%0A%u5728%u533F%u540D%u7C7B%u4E2D%u521D%u59CB%u5316%u53C2%u6570%0A%60%60%60java%0A%0Apublic%20class%20Parcel9%20%7B%0A%09private%20int%20i%20%3D10%3B%0A%09public%20Destination%20destination%28String%20dest%29%7B%0A%09//public%20Destination%20destination%28final%20String%20dest%29%7B%0A%09%09return%20new%20Destination%28%29%20%7B%0A%09%09%09private%20String%20label%20%3D%20dest%3B%0A%09%09%09int%20j%20%3Di%3B%0A%09%09%09@Override%0A%09%09%09public%20String%20readLabel%28%29%20%7B%0A%09%09%09%09//%20TODO%20Auto-generated%20method%20stub%0A%09%09%09%09System.out.println%28j%29%3B%0A%09%09%09%09return%20label%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel9%20p%20%3D%20new%20Parcel9%28%29%3B%0A%09%09Destination%20d%20%3D%20p.destination%28%22USA%22%29%3B%0A%09%09%0A%09%09System.out.println%28d.readLabel%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A%u533F%u540D%u5185%u90E8%u7C7B%u8FD8%u662F%u4E00%u4E2A%u5185%u90E8%u7C7B%2C%u5185%u90E8%u7C7B%u7684%u7279%u6027%u5B83%u8FD8%u662F%u6709%u7684%2C%u6BD4%u5982%u53EF%u4EE5%u65E0%u6761%u4EF6%u7684%u8BBF%u95EE%u5916%u5D4C%u7C7B%u7684%u6210%u5458.%20%u4F46%u662F%u8981%u6C42%u5F62%u53C2%u5F15%u7528%u7C7B%u578B%u5FC5%u987B%u662Ffinal%2C%u4F46%u662F%u6211%u8BD5%u4E86%u4E00%u4E0B%2C1.6%u7248%u672C%u4E2D%u8FD8%u6709%u8FD9%u4E2A%u9650%u5236%2C%u57281.8%u4E2D%u6CA1%u6709%u8FD9%u4E2A%u9650%u5236%u4E86.%20%u8FD9%u79CD%u65B9%u6CD5%u7B80%u5355%u7684%u7ED9%u4E00%u4E2A%u5B57%u6BB5%u8D4B%u503C%u662F%u53EF%u4EE5%u7684%2C%u4F46%u662F%u5982%u679C%u60F3%u8981%u5B8C%u6210%u4E00%u4E9B%u6784%u9020%u5668%u7684%u884C%u4E3A%2C%u600E%u4E48%u529E%u5462%3F%0A%0A%u9996%u5148%u533F%u540D%u5185%u90E8%u7C7B%u4E2D%u662F%u4E0D%u53EF%u80FD%u6709%u547D%u540D%u6784%u9020%u5668%u7684.%20%u4F46%u662F%u901A%u8FC7%u4F7F%u7528%u5B9E%u4F8B%u521D%u59CB%u5316%u5668%u53EF%u4EE5%u8FBE%u5230%u7C7B%u4F3C%u7684%u6548%u679C.%20%0A%60%60%60java%0Aabstract%20class%20Base%7B%0A%09public%20Base%28int%20i%29%7B%0A%09%09System.out.println%28%22Base%20constructor%2C%20i%3D%20%22+i%29%3B%0A%09%7D%0A%09public%20abstract%20void%20f%28%29%3B%0A%7D%0A%0Apublic%20class%20AnonymousConstructor%20%7B%0A%09public%20static%20Base%20getBase%28int%20i%29%7B%0A%09%09return%20new%20Base%28i%29%7B%0A%09%09%09%7BSystem.out.println%28%22inside%20instance%20initializer%22%29%3B%7D%0A%09%09%09public%20void%20f%28%29%7B%0A%09%09%09%09System.out.println%28%22in%20anonymous%20f%28%29%22%29%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Base%20base%20%3D%20AnonymousConstructor.getBase%2847%29%3B%0A%09%09base.f%28%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u518D%u6BD4%u5982%3A%0A%60%60%60java%0Apublic%20class%20Parcel10%20%7B%0A%09public%20Destination%20destination%28String%20dest%2Cfloat%20price%29%7B%0A%09%09return%20new%20Destination%28%29%20%7B%0A%09%09%09private%20float%20cost%3B%0A%09%09%09%7B%0A%09%09%09%09cost%20%3D%20Math.round%28price%29%3B%0A%09%09%09%09if%28cost%3E100%29%7B%0A%09%09%09%09%09System.out.println%28%22over%20budget%22%29%3B%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%09private%20String%20label%20%3D%20dest%3B%0A%09%09%09@Override%0A%09%09%09public%20String%20readLabel%28%29%20%7B%0A%09%09%09%09//%20TODO%20Auto-generated%20method%20stub%0A%09%09%09%09return%20label%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Parcel10%20p%20%3D%20new%20Parcel10%28%29%3B%0A%09%09p.destination%28%22USA%22%2C%20101.123f%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A%u4E0A%u9762%u4EE3%u78015-9%u884C%u662F%u4E00%u4E2A%u5B9E%u4F8B%u521D%u59CB%u5316%u5668%2C%u5B83%u7684%u4F5C%u7528%u5728%u533F%u540D%u5185%u90E8%u7C7B%u4E2D%u5C31%u76F8%u5F53%u4E8E%u4E00%u4E2A%u6784%u9020%u5668.%20%u4F46%u662F%u533F%u540D%u5185%u90E8%u7C7B%u4E2D%u662F%u6709%u9650%u5236%u7684%2C%u6BD4%u5982%2C%u6784%u9020%u5668%u4E0D%u80FD%u91CD%u8F7D%2C%u800C%u4E14%u53EA%u80FD%u7EE7%u627F%u4E00%u4E2A%u7C7B%2C%u6216%u8005%u5B9E%u73B0%u4E00%u4E2A%u63A5%u53E3%2C%u5E76%u4E0D%u80FD%u4E24%u8005%u517C%u5907.%20%0A%0A%23%23%23%u4F7F%u7528%u533F%u540D%u5185%u90E8%u7C7B%u6765%u5B9E%u73B0%u5DE5%u5382%u6A21%u5F0F%0A%60%60%60java%0Ainterface%20Service%20%7B%0A%09void%20method1%28%29%3B%0A%0A%09void%20method2%28%29%3B%0A%7D%0A%0Ainterface%20ServiceFactory%20%7B%0A%09Service%20getService%28%29%3B%0A%7D%0A%0Aclass%20Implementation1%20implements%20Service%20%7B%0A%09private%20Implementation1%28%29%20%7B%0A%09%7D%0A%0A%09@Override%0A%09public%20void%20method1%28%29%20%7B%0A%09%09System.out.println%28%22I1m1%22%29%3B%0A%09%7D%0A%0A%09@Override%0A%09public%20void%20method2%28%29%20%7B%0A%09%09System.out.println%28%22I1m2%22%29%3B%0A%09%7D%0A%0A%09public%20static%20ServiceFactory%20factory%28%29%20%7B%0A%09%09return%20new%20ServiceFactory%28%29%20%7B%0A%09%09%09@Override%0A%09%09%09public%20Service%20getService%28%29%20%7B%0A%09%09%09%09//%20TODO%20Auto-generated%20method%20stub%0A%09%09%09%09return%20new%20Implementation1%28%29%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%0A%7D%0A%0Aclass%20Implementation2%20implements%20Service%20%7B%0A%09private%20Implementation2%28%29%20%7B%0A%09%7D%0A%0A%09@Override%0A%09public%20void%20method1%28%29%20%7B%0A%09%09System.out.println%28%22I2m1%22%29%3B%0A%09%7D%0A%0A%09@Override%0A%09public%20void%20method2%28%29%20%7B%0A%09%09System.out.println%28%22I2m2%22%29%3B%0A%09%7D%0A%0A%09public%20static%20ServiceFactory%20factory%28%29%20%7B%0A%09%09return%20new%20ServiceFactory%28%29%20%7B%0A%09%09%09@Override%0A%09%09%09public%20Service%20getService%28%29%20%7B%0A%09%09%09%09//%20TODO%20Auto-generated%20method%20stub%0A%09%09%09%09return%20new%20Implementation2%28%29%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%0A%7D%0A%0Apublic%20class%20Factories%20%7B%0A%09public%20static%20void%20serviceConsumer%28ServiceFactory%20fact%29%20%7B%0A%09%09Service%20s%20%3D%20fact.getService%28%29%3B%0A%09%09s.method1%28%29%3B%0A%09%09s.method2%28%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09serviceConsumer%28Implementation1.factory%28%29%29%3B%0A%09%09serviceConsumer%28Implementation2.factory%28%29%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A%u4E0A%u4E00%u7AE0%u4E2D%u8BF4%u5230%u4E86%u5DE5%u5382%u6A21%u5F0F%2C%u90A3%u4E2A%u65F6%u5019%u8FD8%u9700%u8981%u521B%u5EFA%u4E00%u4E2A%60Implementation1Factory%60%u7684%u7C7B%2C%u6709%u4E86%u533F%u540D%u5185%u90E8%u7C7B%u4E4B%u540E%2C%u8FD9%u4E2A%u5C31%u4E0D%u9700%u8981%u521B%u5EFA%u4E86.%20%u50CF25%u884C%2C51%u884C%u4F7F%u7528%u4E86%u533F%u540D%u5185%u90E8%u7C7B.%20%0A%0A%0A%23%23%u5D4C%u5957%u7C7B%0A%u5982%u679C%u4E0D%u9700%u8981%u5185%u90E8%u7C7B%u5BF9%u8C61%u4E0E%u5916%u56F4%u7C7B%u5BF9%u8C61%u4E4B%u95F4%u6709%u8054%u7CFB%2C%u90A3%u53EF%u4EE5%u5C06%u5185%u90E8%u7C7B%u5BF9%u8C61%u58F0%u660E%u4E3Astatic.%20%u8FD9%u6837%u5C31%u6709%u4E86%u5D4C%u5957%u7C7B%2C%u4E5F%u53EB%u9759%u6001%u5185%u90E8%u7C7B.%20%u975Estatic%u4FEE%u9970%u7684%u5185%u90E8%u7C7B%u90FD%u4F1A%u4FDD%u6709%u5916%u5D4C%u7C7B%u7684this%u6307%u9488.%20%u4F46%u662F%u5D4C%u5957%u5185%u90E8%u7C7B%u6CA1%u6709%u8FD9%u4E2A%u6307%u9488.%20%u6240%u4EE5%u4E00%u4E2A%u5185%u5D4C%u7C7B%u770B%u4E0A%u53BB%u6709%u70B9%u7C7B%u4F3C%u4E00%u4E2Astatic%u65B9%u6CD5.%20%u57C3%u5927%u7237%u8BF4%3A%0A1.%20%u8981%u521B%u5EFA%u5D4C%u5957%u7C7B%u7684%u5BF9%u8C61%2C%u4E0D%u9700%u8981%u5176%u5916%u56F4%u7C7B%u7684%u5BF9%u8C61.%0A2.%20%u4E0D%u80FD%u4ECE%u5D4C%u5957%u7C7B%u5BF9%u8C61%u4E2D%u8BBF%u95EE%u975E%u9759%u6001%u7684%u5916%u56F4%u7C7B%u7684%u5BF9%u8C61.%20%0A%0A%u5D4C%u5957%u7C7B%u4E0E%u666E%u901A%u5185%u90E8%u7C7B%u8FD8%u6709%u4E00%u4E2A%u533A%u522B%2C%u4E5F%u662F%u4E4B%u524D%u6CA1%u6709%u8BF4%u9053%u7684%u4E00%u4E2A%u9650%u5236.%20%u666E%u901A%u7684%u5185%u90E8%u7C7B%u4E2D%u4E0D%u80FD%u542B%u6709static%u6210%u5458.%u4E5F%u5C31%u662F%u6240%u6709static%u6210%u5458%u5FC5%u987B%u653E%u5728%u5916%u5D4C%u7C7B%u4E2D%u751F%u547D.%20%u4F46%u662F%u5D4C%u5957%u7C7B%u4E2D%u662F%u53EF%u4EE5%u58F0%u660Estatic%u6210%u5458%u7684.%20%0A%60%60%60java%0Apublic%20class%20Parcel11%20%7B%0A%09//nested%20classes%0A%09private%20static%20class%20ParcelContents%20implements%20Contents%7B%0A%09%09private%20int%20i%20%3D11%3B%0A%09%09@Override%0A%09%09public%20int%20value%28%29%20%7B%0A%09%09%09return%20i%3B%0A%09%09%7D%0A%09%7D%0A%09%0A%09protected%20static%20class%20ParcelDestination%20implements%20Destination%7B%0A%09%09String%20label%3B%0A%09%09private%20%20ParcelDestination%28String%20whereTo%29%20%7B%0A%09%09%09label%20%3D%20whereTo%3B%0A%09%09%7D%0A%09%09@Override%0A%09%09public%20String%20readLabel%28%29%20%7B%0A%09%09%09%0A%09%09%09return%20label%3B%0A%09%09%7D%09%0A%09%09public%20static%20void%20f%28%29%7B%0A%09%09%09System.out.println%28%22f%28%29%22%29%3B%0A%09%09%7D%0A%09%09//we%20can%20define%20static%20members%20in%20a%20nested%20class%0A%09%09static%20int%20x%20%3D10%3B%0A%09%09static%20class%20AnotherLevel%7B%0A%09%09%09public%20static%20void%20f%28%29%7B%0A%09%09%09%09System.out.println%28%22f%28%29%22%29%3B%0A%09%09%09%7D%0A%09%09%09static%20int%20x%20%3D10%3B%0A%09%09%7D%0A%09%7D%0A%09public%20static%20Destination%20destination%28String%20s%29%7B%0A%09%09return%20new%20ParcelDestination%28s%29%3B%0A%09%7D%0A%09public%20static%20Contents%20contents%28%29%7B%0A%09%09return%20new%20ParcelContents%28%29%3B%0A%09%7D%0A%09%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Contents%20c%20%3D%20contents%28%29%3B%0A%09%09Destination%20d%20%3D%20destination%28%22USA%22%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u5D4C%u5957%u7C7B%u5927%u6982%u5C31%u662F%u8FD9%u4E2A%u6837%u5B50%2C%u6CE8%u610F%21%21%21%0A-%20main%u65B9%u6CD5%u4E2D%u5E76%u6CA1%u6709%u521B%u5EFA%u5916%u5D4C%u7C7B%u7684%u5B9E%u4F8B%2C%u4F46%u662F%u4F9D%u65E7%u53EF%u4EE5%u521B%u5EFA%u5185%u5D4C%u7C7B%u7684%u5B9E%u4F8B.%20%0A-%2025%2C26%u884C%2C%u5728%u4E00%u4E2A%u5185%u5D4C%u7C7B%u7684%u5185%u90E8%u521B%u5EFA%u4E86static%u6210%u5458.%20%0A%0A**%u63A5%u53E3%u5185%u90E8%u7684%u7C7B**%0A%u6211%u4EEC%u6765%u505A%u4E00%u4E2A%u4E0D%u4E25%u8C28%u7684%u63A8%u5BFC%2C%u6240%u6709%u7C7B%u90FD%u53EF%u4EE5%u521B%u5EFA%u5185%u90E8%u7C7B--%3Ejava%u652F%u6301%u5185%u5D4C%u7C7B%28%u8BF4%u8D77%u6765%u597D%u522B%u626D%2C%u611F%u89C9%u9759%u6001%u5185%u90E8%u7C7B%u542C%u8D77%u6765%u597D%u542C%u4E00%u70B9...%29--%3E%u63A5%u53E3%u662F%u4E00%u79CD%u7279%u6B8A%u7684%u7C7B--%3E%u6240%u4EE5%u63A5%u53E3%u91CC%u4E5F%u53EF%u4EE5%u58F0%u660E%u5185%u5D4C%u7C7B.%20%0A%u9996%u5148%2C%u6211%u4EEC%u8981%u660E%u786E%u4E00%u4E2A%u539F%u5219%2C%u63A5%u53E3%u66F4%u591A%u7684%u662F%u63D0%u4F9B%u4E00%u79CD%u534F%u8BAE%2C%u4ED6%u5176%u4E2D%u4E0D%u5E94%u8BE5%u51FA%u73B0%u4EFB%u4F55%u5B9E%u73B0%u76F8%u5173%u7684%u4EE3%u7801.%20%u4F46%u662F%2C%u5D4C%u5957%u7C7B%u53EF%u4EE5%u4F5C%u4E3A%u63A5%u53E3%u7684%u4E00%u90E8%u5206.%20%u63A5%u53E3%u4E2D%u6240%u6709%u6210%u5458%u90FD%u662F%u9690%u5F0Fpublic%20static%u7684.%20%u65E2%u7136%u90FD%u662Fstatic%u7684%2C%u5B83%u53EF%u4EE5%u662Fstatic%u7684%u53D8%u91CF%2C%u4E5F%u53EF%u4EE5%u662Fstatic%u7684%u65B9%u6CD5%2C%u90A3%u4E48%u4E3A%u4EC0%u4E48%u4E0D%u53EF%u4EE5%u662Fstatic%u7684%u5185%u90E8%u7C7B%u5462%3F%0A%0A%60%60%60java%0Apublic%20interface%20ClassInInterface%20%7B%0A%09void%20howdy%28%29%3B%0A%09class%20Test%20implements%20ClassInInterface%7B%0A%09%09@Override%0A%09%09public%20void%20howdy%28%29%20%7B%0A%09%09%09System.out.println%28%22Howdy%21%22%29%3B%0A%09%09%7D%0A%09%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09%09new%20Test%28%29.howdy%28%29%3B%0A%09%09%7D%0A%09%7D%0A%7D%0A%60%60%60%20%0A%u50CF%u8FD9%u4E2A%u6837%u5B50.%20%u57C3%u5927%u7237%u8BF4%u5982%u679C%u60F3%u7ED9%u4E00%u4E2A%u63A5%u53E3%u7684%u6240%u6709%u5B9E%u73B0%u63D0%u4F9B%u516C%u5171%u7684%u4EE3%u7801%2C%u53EF%u4EE5%u4F7F%u7528%u8FD9%u79CD%u65B9%u5F0F.%20%u4F46%u662F%u6211%u5B9E%u5728%u662F%u7406%u89E3%u4E0D%u4E86...%0A%0A%0A**%u4ECE%u591A%u5C42%u5D4C%u5957%u7C7B%u4E2D%u8BBF%u95EE%u5916%u90E8%u7C7B%u7684%u6210%u5458**%0A%u4E00%u53E5%u8BDD%2C%u4E00%u4E2A%u5185%u90E8%u7C7B%u88AB%u5D4C%u5957%u591A%u5C11%u5C42%u90FD%u4E0D%u91CD%u8981%2C%u5B83%u90FD%u53EF%u4EE5%u900F%u660E%u7684%u8BBF%u95EE%u6240%u6709%u5916%u90E8%u7C7B%u7684%u6210%u5458.%20%0A%60%60%60java%0Aclass%20MNA%7B%0A%09private%20void%20f%28%29%7B%0A%09%09System.out.println%28%22111%22%29%3B%0A%09%7D%0A%09class%20A%7B%0A%09%09private%20void%20g%28%29%7B%0A%09%09%09System.out.println%28%22222%22%29%3B%0A%09%09%7D%0A%09%09public%20class%20B%7B%0A%09%09%09void%20h%28%29%7B%0A%09%09%09%09g%28%29%3B%0A%09%09%09%09f%28%29%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%7D%0Apublic%20class%20MultiNestingAccess%20%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09MNA%20mna%20%3D%20new%20MNA%28%29%3B%0A%09%09MNA.A%20mnaa%20%3D%20mna.new%20A%28%29%3B%0A%09%09MNA.A.B%20mnaab%20%3D%20mnaa.new%20B%28%29%3B%0A%09%09mnaab.h%28%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%23%23%u5185%u90E8%u7C7B%u7684%u7EE7%u627F%0A%u5185%u90E8%u7C7B%u7684%u6784%u9020%u5668%u5FC5%u987B%u8981%u8FDE%u63A5%u5230%u6307%u5411%u5916%u90E8%u7C7B%u7684%u5F15%u7528%2C%u4E5F%u5C31%u662F%u90A3%u4E2A%60.new%60%u8BED%u6CD5%u6240%u4EE5%u5982%u679C%u60F3%u8981%u7EE7%u627F%u5185%u90E8%u7C7B%u7684%u65F6%u5019%2C%u8FD9%u4E2A%u6307%u5411%u5916%u5D4C%u7C7B%u5BF9%u8C61%u7684%u9690%u5F0F%u7684%u5F15%u7528%u5FC5%u987B%u88AB%u521D%u59CB%u5316%2C%u800C%u5728%u5B50%u7C7B%u4E2D%u4E0D%u518D%u5B58%u5728%u53EF%u8FDE%u63A5%u7684%u9ED8%u8BA4%u5BF9%u8C61.%20%u8FD9%u4E2A%u65F6%u5019%u5C31%u9700%u8981%u7528%u4E00%u4E9B%u7279%u6B8A%u624B%u6BB5%u4E86.%20%28%u8FD9%u4E48%u590D%u6742%u7684%u60C5%u51B5%u5DF2%u7ECF%u5B8C%u5168%u8D85%u51FA%u6211%u7684%u8BA4%u77E5%u8303%u56F4%u4E86%2C%u8BF7%u81EA%u884C%u8111%u8865%u770B%u5230%u8FD9%u4E00%u8282%u7684%u65F6%u5019%2C%u6211%u7684%u8868%u60C5%29%0A%0A%60%60%60java%0Aclass%20WithInner%7B%0A%09class%20Inner%7B%7D%0A%7D%0Apublic%20class%20InheritInner%20extends%20WithInner.Inner%7B%0A%09//can%20not%20use%0A%09//public%20InheritInner%28%29%20%7B%7D%0A%09public%20InheritInner%28WithInner%20wi%29%20%7B%0A%09%09wi.super%28%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09%0A%09%09WithInner%20wi%20%3D%20new%20WithInner%28%29%3B%0A%09%09//must%20use%20outer%20class%27%20reference%20to%20complete%20the%20construction%0A%09%09InheritInner%20ii%20%3D%20new%20InheritInner%28wi%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u6CE8%u610F7-9%u884C%u7684%u4EE3%u7801%2C%u7EE7%u627F%u4E00%u4E2A%u5185%u90E8%u7C7B%u7684%u65F6%u5019%2C%u6784%u9020%u65B9%u6CD5%u5FC5%u987B%u4F20%u9012%u4E00%u4E2A%u5916%u90E8%u7C7B%u7684%u5F15%u7528%2C%u5E76%u4E14%u4F7F%u7528super%28%29%u8BED%u6CD5%u8FDB%u884C%u521D%u59CB%u5316.%0A%0A%23%23%u8986%u76D6%u5185%u90E8%u7C7B%0A%u5185%u90E8%u7C7B%u6709%u65F6%u5019%u770B%u4E0A%u53BB%u66F4%u50CF%u662F%u4E00%u4E2A%u65B9%u6CD5%2C%u90A3%u4E48%u6709%u6CA1%u6709%22%u91CD%u5199%22%u4E00%u8BF4%u5462%3F%20%20%u518D%u63A8%u5BFC%u4E00%u4E0B%2C%u5185%u90E8%u7C7B%u770B%u4E0A%u53BB%u50CF%u4E00%u4E2A%u6210%u5458%u65B9%u6CD5%2C%u6210%u5458%u65B9%u6CD5%u662F%u53EF%u4EE5%u88AB%u91CD%u5199%u7684%2C%u90A3%u4E48%u5185%u90E8%u7C7B%u5728%u7EE7%u627F%u7684%u65F6%u5019%2C%u4E5F%u662F%u53EF%u4EE5%u88AB%u8986%u76D6%u7684%2C%u662F%u5417%3F%21%20%u7801%u4E00%u53D1%21%0A%60%60%60java%0Aclass%20Egg%7B%0A%09private%20Yolk%20y%3B%0A%09protected%20class%20Yolk%7B%0A%09%09public%20Yolk%28%29%7B%0A%09%09%09System.out.println%28%22Egg.Yolk%28%29%22%29%3B%0A%09%09%7D%0A%09%7D%0A%09public%20Egg%28%29%7B%0A%09%09System.out.println%28%22New%20Egg%28%29%22%29%3B%0A%09%09y%3Dnew%20Yolk%28%29%3B%0A%09%7D%0A%7D%0Apublic%20class%20BigEgg%20extends%20Egg%7B%0A%09public%20class%20Yolk%7B%0A%09%09public%20Yolk%28%29%7B%0A%09%09%09System.out.println%28%22BigEgg.Yolk%28%29%22%29%3B%0A%09%09%7D%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09new%20BigEgg%28%29%3B%0A%09%7D%0A%7D/*output%0ANew%20Egg%28%29%0AEgg.Yolk%28%29%0A*/%0A%60%60%60%0A%u4E0D%u597D%u610F%u601D%2C**%u6CA1%u6709%u88AB%u8986%u76D6.**%20%20%u8FD9%u662F%u4E3A%u5565%u5462%3F%0A%u8FD9%u8981%u4ECE%u5185%u90E8%u7C7B%u7F16%u8BD1%u7684%u4EA7%u7269%u6765%u8BF4%u4E86.%20%u4E00%u4E2A%u5305%u542B%u5185%u90E8%u7C7B%u7684%u7C7B%28%u57C3%u5927%u7237%u8BF4%u7684%u5F88%u5B66%u672F%u8303%u513F%2C%u53EB%u7F16%u8BD1%u5355%u5143%2C%u5C31%u662F%u4E2A.java%u7684%u6E90%u6587%u4EF6%29%u7F16%u8BD1%u5B8C%u4E4B%u540E%u4F1A%u751F%u6210%u7C7B%u4F3C%u90A3%u4E48%u51E0%u4E2A%u73A9%u610F%u513F.%20%u5185%u90E8%u7C7B%u4F1A%u662F%u4E00%u4E2A%u72EC%u7ACB%u7684class%u6587%u4EF6%2C%u4F46%u662F%u547D%u540D%u4E0A%u4F1A%u4EE5%60%24%60%u6765%u8FDE%u63A5%u5916%u5D4C%u7C7B%u548C%u5185%u90E8%u7C7B.%20%u5BF9%u4E8E%u533F%u540D%u5185%u90E8%u7C7B%2C%60%24%60%u540E%u662F%u4E00%u4E32%u6570%u5B57.%0A%60Parcel1.class%60%0A%60Parcel1%24Destination.class%60%0A%60Parcel1%24Contents.class%60%0A%0A%u4E5F%u5C31%u662F%u8BF4%2C%u5916%u5D4C%u7C7B%u548C%u5185%u90E8%u7C7B%u7F16%u8BD1%u4E4B%u540E%u5176%u5B9E%u662F%u4E24%u4E2A%u72EC%u7ACB%u7684%u5B9E%u4F53%2C%u6709%u7740%u5404%u81EA%u7684%u547D%u540D%u7A7A%u95F4.%20%u5B83%u4E0D%u5B58%u5728%u88AB%u91CD%u5199%u4E00%u8BF4.%20%u4F46%u662F%u80FD%u4E0D%u80FD%u8FBE%u5230%u4E00%u4E2A%u91CD%u5199%u7684%u6548%u679C%u5462%2C%u4E0D%u7528%u72B9%u8C6B%2C%u80AF%u5B9A%u80FD.%20%0A%0A%60%60%60java%0Aclass%20Egg2%7B%0A%09private%20Yolk%20y%3Dnew%20Yolk%28%29%3B%0A%09protected%20class%20Yolk%7B%0A%09%09public%20Yolk%28%29%7B%0A%09%09%09System.out.println%28%22Egg2.Yolk%28%29%22%29%3B%0A%09%09%7D%0A%09%09public%20void%20f%28%29%7B%0A%09%09%09System.out.println%28%22Egg2.Yolk.f%28%29%22%29%3B%0A%09%09%7D%0A%09%7D%0A%09public%20Egg2%28%29%7B%0A%09%09System.out.println%28%22New%20Egg2%28%29%22%29%3B%0A%09%7D%0A%09public%20void%20insertYolk%28Yolk%20yy%29%7B%0A%09%09y%3Dyy%3B%0A%09%7D%0A%09public%20void%20g%28%29%7B%0A%09%09y.f%28%29%3B%0A%09%7D%0A%09%0A%7D%0A%0Apublic%20class%20BigEgg2%20extends%20Egg2%7B%0A%09public%20class%20Yolk%20extends%20Egg2.Yolk%7B%0A%09%09public%20Yolk%28%29%7B%0A%09%09%09System.out.println%28%22BigEgg2.Yolk%28%29%22%29%3B%0A%09%09%7D%0A%09%09public%20void%20f%28%29%7B%0A%09%09%09System.out.println%28%22BigEgg2.Yolk.f%28%29%22%29%3B%0A%09%09%7D%0A%09%7D%0A%09public%20BigEgg2%28%29%7BinsertYolk%28new%20Yolk%28%29%29%3B%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Egg2%20e2%3Dnew%20BigEgg2%28%29%3B%0A%09%09e2.g%28%29%3B%0A%09%7D%0A%7D/*output%0AEgg2.Yolk%28%29%0ANew%20Egg2%28%29%0AEgg2.Yolk%28%29%0ABigEgg2.Yolk%28%29%0ABigEgg2.Yolk.f%28%29%0A*/%0A%60%60%60%0A%0A%23%23%u5C40%u90E8%u5185%u90E8%u7C7B%u548C%u533F%u540D%u5185%u90E8%u7C7B%u7684%u533A%u522B%0A%u5C40%u90E8%u5185%u90E8%u7C7B%u4E0D%u80FD%u6709%u6743%u9650%u4FEE%u9970%u7B26%2C%u56E0%u4E3A%u5B83%u4E0D%u662F%u5916%u5D4C%u7C7B%u7684%u4E00%u90E8%u5206.%20%u4F46%u662F%u5B83%u53EF%u4EE5%u8BBF%u95EE%u5F53%u524D%u4EE3%u7801%u5757%u7684%u5E38%u91CF%u8FD8%u6709%u5916%u5D4C%u7C7B%u7684%u6240%u6709%u6210%u5458.%20%0A%60%60%60java%0Ainterface%20Counter%7B%0A%09int%20next%28%29%3B%0A%7D%0Apublic%20class%20LocalInnerClass%20%7B%0A%09private%20int%20count%3D0%3B%0A%09%0A%09Counter%20getCounter%28String%20name%29%7B%0A%09%09class%20LocalCounter%20implements%20Counter%7B%0A%09%09%09public%20LocalCounter%28%29%7B%0A%09%09%09%09System.out.println%28%22LocalCounter%28%29%22%29%3B%0A%09%09%09%7D%0A%09%09%09@Override%0A%09%09%09public%20int%20next%28%29%7B%0A%09%09%09%09System.out.print%28name+%22%20%22%29%3B%0A%09%09%09%09return%20count++%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return%20new%20LocalCounter%28%29%3B%0A%09%7D%0A%09%0A%09Counter%20getCounter2%28String%20name%29%7B%0A%09%09return%20new%20Counter%28%29%7B%0A%09%09%09%7B%0A%09%09%09%09System.out.println%28%22Counter%28%29%22%29%3B%0A%09%09%09%7D%0A%09%09%09@Override%0A%09%09%09public%20int%20next%28%29%20%7B%0A%09%09%09%09System.out.print%28name+%22%20%22%29%3B%0A%09%09%09%09return%20count++%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09LocalInnerClass%20lic%20%3D%20new%20LocalInnerClass%28%29%3B%0A%09%09Counter%20c1%20%3D%20lic.getCounter%28%22local%20innner%22%29%3B%0A%09%09Counter%20c2%20%3D%20lic.getCounter2%28%22Anonymous%20inner%22%29%3B%0A%09%09%0A%09%09for%20%28int%20i%20%3D0%3Bi%3C5%3Bi++%29%7B%0A%09%09%09System.out.println%28c1.next%28%29%29%3B%0A%09%09%7D%0A%09%09for%28int%20i%20%3D0%3Bi%3C5%3Bi++%29%7B%0A%09%09%09System.out.println%28c2.next%28%29%29%3B%0A%09%09%7D%0A%09%7D%0A%7D/*output%0ALocalCounter%28%29%0ACounter%28%29%0Alocal%20innner%200%0Alocal%20innner%201%0Alocal%20innner%202%0Alocal%20innner%203%0Alocal%20innner%204%0AAnonymous%20inner%205%0AAnonymous%20inner%206%0AAnonymous%20inner%207%0AAnonymous%20inner%208%0AAnonymous%20inner%209%0A*/%0A%60%60%60%0A%0A%u4ECE%u4EE3%u7801%u4E0A%u770B%2C%u8FD9%u4E00%u6BB5%u4EE3%u7801%u533F%u540D%u5185%u90E8%u7C7B%u548C%u5C40%u90E8%u5185%u90E8%u7C7B%u5B9E%u73B0%u4E86%u76F8%u540C%u7684%u529F%u80FD.%20%u90A3%u4E48%u4ED6%u4EEC%u7684%u533A%u522B%u5728%u54EA%u91CC%3F%u4F55%u65F6%u7528%u533F%u540D%u5185%u90E8%u7C7B%u4F55%u65F6%u7528%u5C40%u90E8%u5185%u90E8%u7C7B%3F%20%u5982%u679C%u6211%u4EEC%u9700%u8981%u7528%u4E00%u4E2A%u547D%u540D%u6784%u9020%u5668%2C%u6216%u8005%u9700%u8981%u91CD%u8F7D%u6784%u9020%u5668%u7684%u65F6%u5019%2C%u4F7F%u7528%u5C40%u90E8%u5185%u90E8%u7C7B%2C%u56E0%u4E3A%u533F%u540D%u5185%u90E8%u7C7B%u53EA%u80FD%u5B8C%u6210%u5B9E%u4F8B%u7684%u521D%u59CB%u5316.%20%0A%0A

TJI读书笔记13-内部类的更多相关文章

  1. TJI读书笔记14-闭包与回调

      TJI读书笔记14-闭包与回调 闭包与回调 为什么要使用内部类?内部类继承自某个类或者实现某个接口,内部类的代码可以操作外嵌类的对象. 这不是使用内部类的理由. 那么为什么使用内部类呢? 我觉得如 ...

  2. TJI读书笔记17-字符串

    TJI读书笔记17-字符串 不可变的String 重载”+”和StringBuilder toString()方法的一个坑 String上的操作 格式化输出 Formatter类 字符串操作可能是计算 ...

  3. TJI读书笔记16-异常处理

    TJI读书笔记16-异常处理 概念 基本异常情形 异常的捕获 自定义异常 异常说明 捕获所有异常 栈轨迹 重新抛出异常 Java标准异常 使用finally 异常的限制 构造器 异常的匹配 其他乱七八 ...

  4. TJI读书笔记15-持有对象

    TJI读书笔记15-持有对象 总览 类型安全和泛型 Collection接口 添加元素 List 迭代器 LinkedList 栈 Set Map Queue Collection和Iterator ...

  5. TJI读书笔记12-接口

    TJI读书笔记12-接口 抽象类和抽象方法 接口 完全解耦和策略模式 接口间的继承关系 工厂模式 乱七八糟不知道怎么归类的知识点 接口和抽象类为我们提供了更强又有力的接口和实现分离的方法. 抽象类和抽 ...

  6. TJI读书笔记11-多态

    TJI读书笔记11-多态 再说说向上转型 多态的原理 构造器和多态 协变返回类型 使用继承进行设计 多态是数据抽象和继承之后的第三种基本特征. 一句话说,多态分离了做什么和怎么做(再次对埃大爷佩服的五 ...

  7. TJI读书笔记10-复用类

    TJI读书笔记10-复用类 组合语法 继承语法 代理 final关键字 final的数据 final的参数 final的方法 final的类 初始化和类的加载 乱七八糟不知道怎么归类的知识点 代码复用 ...

  8. TJI读书笔记09-访问控制权限

    TJI读书笔记09-访问控制权限 包,package和import 权限修饰符 接口和实现 类的访问权限控制 首先问一个问题,为什么要有访问控制权限? 安全,这当然是一个很重要的原因. 让类库的使用者 ...

  9. TJI读书笔记07-初始化

    TJI读书笔记07-初始化 成员初始化 构造方法初始化 初始化块 初始化的顺序 成员初始化 java尽量去保证每个变量在使用前都会得到初始化. 对于方法局部变量,java不会自动初始化他们,如果没有显 ...

随机推荐

  1. SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件

    SQL SERVER 2008配置Database Mail –用SQL  数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...

  2. Spring 核心概念以及入门教程

    初始Spring 在学习Spring之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...

  3. 【转】15个无比华丽的HTML5/CSS3动画应用

    原文转自:http://www.html5cn.org/article-7089-1.html 前几天,HTML5标准已经尘埃落定,未来的Web将会是由HTML5主导,当然作为开发者对这一喜讯更为动心 ...

  4. ArrayBlockingQueue-我们到底能走多远系列(42)

    我们到底能走多远系列(42) 扯淡: 乘着有空,读些juc的源码学习下.后续把juc大致走一边,反正以后肯定要再来. 主题: BlockingQueue 是什么 A java.util.Queue t ...

  5. Python 爬虫学习 urllib

    网页抓取 # -*-coding: utf-8 -*- import urllib url = "http://www.cndzz.com/" html = urllib.urlo ...

  6. github 如何合并不同分支

    From: http://stackoverflow.com/questions/1123344/merging-between-forks-in-github 1. 添加remote origina ...

  7. [原创]cocos2d-x研习录-第二阶 概念类之摄相机类(CCCamera)

    在Cocos2D-x中,每个CCNode都拥有一个摄像机类CCCamera.只有通过CCCamera,CCNode才会被渲染出来.当CCNode发生缩放.旋转和位置变化时,都需要覆盖CCCamera, ...

  8. JAVA 对象引用,以及对象赋值

    注:引自http://zwmf.iteye.com/blog/1738574 关键字: java对象 引用 Java对象及其引用 关于对象与引用之间的一些基本概念. 初学Java时,在很长一段时间里, ...

  9. [redis] redis.clients.jedis.exceptions.JedisDataException: MOVED 13102 127.0.0.1

    这个异常上网查了很久才知道原因: MOVED indicates that you're using Redis Cluster. ShardedJedis is not for Redis Clus ...

  10. 使用Script Component源处理不规则平面文件

    微软 BI 系列随笔 - SSIS 2012 高级应用 - Script Component处理不规则平面文件 场景介绍 在使用SSIS从平面文件导入源数据时,最常遇到的是以下两种情况: 导入规则的平 ...