File tree 13 files changed +424
-0
lines changed
src/test/java/org/thinkinjava
13 files changed +424
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .initialization ;
2
+
3
+ /**
4
+ * 实例初始化子句
5
+ * {
6
+ * xxx;
7
+ * xxx;
8
+ * }
9
+ */
10
+ class Mug {
11
+ Mug (int marker ) {
12
+ System .out .println ("Mug(" + marker + ")" );
13
+ }
14
+
15
+ void f (int marker ) {
16
+ System .out .println ("f(" + marker + ")" );
17
+ }
18
+ }
19
+
20
+
21
+ public class Mugs {
22
+ Mug mug1 ;
23
+ Mug mug2 ;
24
+ {
25
+ mug1 = new Mug (1 );
26
+ mug2 = new Mug (2 );
27
+ System .out .println ("mug1 & mug2 initialized" );
28
+ }
29
+
30
+ Mugs () {
31
+ System .out .println ("Mugs()" );
32
+ }
33
+
34
+ Mugs (int marker ){
35
+ System .out .println ("Mugs(int)" );
36
+ }
37
+
38
+ public static void main (String [] args ) {
39
+ System .out .println ("Inside main()" );
40
+ new Mugs ();
41
+ System .out .println ("new Mugs() completed" );
42
+ new Mugs (1 );
43
+ System .out .println ("new Mugs(1) completed" );
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .initialization ;
2
+
3
+ public class OverloadingVarargs2 {
4
+ static void f (float i , Character ... args ){
5
+ System .out .println ("first" );
6
+ }
7
+ static void f (Character ... args ){
8
+ System .out .println ("second" );
9
+ }
10
+
11
+ public static void main (String [] args ) {
12
+ f (1 , 'a' );
13
+ f ('a' , 'b' );
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .initialization ;
2
+
3
+ /**
4
+ * 初始化的顺序是
5
+ * 先初始化静态对象
6
+ * 类里面的其它属性会按照定义的顺序来初始化
7
+ */
8
+ class Bowl {
9
+ Bowl (int marker ) {
10
+ System .out .println ("Bowl(" + marker + ")" );
11
+ }
12
+
13
+ void f1 (int marker ){
14
+ System .out .println ("f1(" + marker + ")" );
15
+ }
16
+ }
17
+
18
+ class Table {
19
+ static Bowl bowl1 = new Bowl (1 );
20
+ Table () {
21
+ System .out .println ("Table()" );
22
+ bowl1 .f1 (1 );
23
+ }
24
+ void f2 (int marker ) {
25
+ System .out .println ("f2(" + marker + ")" );
26
+ }
27
+ static Bowl bowl2 = new Bowl (2 );
28
+ }
29
+
30
+ class Cupboard {
31
+ Bowl bowl3 = new Bowl (3 );
32
+ static Bowl bowl4 = new Bowl (4 );
33
+
34
+ Cupboard () {
35
+ System .out .println ("Cupboard()" );
36
+ bowl4 .f1 (2 );
37
+ }
38
+ void f3 (int marker ) {
39
+ System .out .println ("f3(" + marker + ")" );
40
+ }
41
+ static Bowl bowl5 = new Bowl (5 );
42
+ }
43
+ public class StaticInitialization {
44
+ public static void main (String [] args ) {
45
+ System .out .println ("Creating new Cupboard() in main" );
46
+ new Cupboard ();
47
+ System .out .println ("Creating new Cupboard() in main" );
48
+ new Cupboard ();
49
+ table .f2 (1 );
50
+ cupboard .f3 (1 );
51
+ }
52
+
53
+ static Table table = new Table ();
54
+ static Cupboard cupboard = new Cupboard ();
55
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses ;
2
+
3
+ /**
4
+ * 练习1
5
+ * 编写一个名为Outer的类,它包含一个名为Inner的类。
6
+ * 在Outer中添加一个方法,它返回一个Inner类型的对象。
7
+ * 在main()中,创建并初始化一个指向某个Inner对象的引用。
8
+ */
9
+
10
+ public class Outer {
11
+
12
+ class Inner {
13
+
14
+ }
15
+ public Inner inners (){
16
+ return new Inner ();
17
+ }
18
+
19
+ public static void main (String [] args ) {
20
+ // 先创建外部类对象
21
+ Outer outer = new Outer ();
22
+
23
+ Outer .Inner inner = outer .inners ();
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses ;
2
+
3
+ //: innerclasses/Sequence.java
4
+ // Holds a sequence of Objects.
5
+
6
+ interface Selector {
7
+ boolean end ();
8
+ Object current ();
9
+ void next ();
10
+ }
11
+
12
+ public class Sequence {
13
+ private Object [] items ;
14
+ private int next = 0 ;
15
+ public Sequence (int size ) { items = new Object [size ]; }
16
+ public void add (Object x ) {
17
+ if (next < items .length )
18
+ items [next ++] = x ;
19
+ }
20
+ private class SequenceSelector implements Selector {
21
+ private int i = 0 ;
22
+ public boolean end () { return i == items .length ; }
23
+ public Object current () { return items [i ]; }
24
+ public void next () { if (i < items .length ) i ++; }
25
+
26
+ /**
27
+ * 练习四
28
+ * 在Sequence.SequenceSelector类中增加一个方法,它可以生成堆外部类Sequence的引用
29
+ */
30
+ public Sequence sequence (){
31
+ return Sequence .this ;
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
+ Selector selector = sequence .selector ();
42
+ while (!selector .end ()) {
43
+ System .out .print (selector .current () + " " );
44
+ selector .next ();
45
+ }
46
+ }
47
+ } /* Output:
48
+ 0 1 2 3 4 5 6 7 8 9
49
+ */ //:~
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses ;
2
+
3
+ /**
4
+ * 练习2
5
+ * 创建一个类,它持有一个String,并且有一个显示这个String的toString()方法。
6
+ * 将你的新类的若干个对象添加到一个Sequence对象中,然后显示它们。
7
+ */
8
+ class StringObject {
9
+ public String name ;
10
+
11
+ public StringObject (String name ){
12
+ this .name = name ;
13
+ }
14
+ public String toString (){
15
+ return name ;
16
+ }
17
+ }
18
+ public class exercise02 {
19
+
20
+ public static void main (String [] args ) {
21
+ Sequence sequence = new Sequence (5 );
22
+
23
+ for (int i = 0 ; i < 5 ; i ++){
24
+ sequence .add (new StringObject ("obj" + i ));
25
+ }
26
+
27
+ Selector selector = sequence .selector ();
28
+
29
+ while (!selector .end ()){
30
+ System .out .println (selector .current ());
31
+ selector .next ();
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses ;
2
+
3
+ /**
4
+ * 练习五
5
+ * 创建一个包含内部类的类,
6
+ * 在另一个独立的类中,创建此内部类的实例
7
+ */
8
+ class OutA {
9
+ class InnerA {
10
+ void print (){
11
+ System .out .println ("--------" );
12
+ }
13
+ }
14
+ }
15
+ public class exercise05 {
16
+ public static void main (String [] args ) {
17
+ OutA outA = new OutA ();
18
+
19
+ OutA .InnerA oia = outA .new InnerA ();
20
+
21
+ oia .print ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses .exercise6 .package1 ;
2
+
3
+ /**
4
+ * 练习六
5
+ * 在第一个包中创建一个至少有一个方法的接口。
6
+ * 然后在第二个包内创建一个类,
7
+ * 在其中增加一个protected的内部类以实现那个接口。
8
+ * 在第三个包中,继承这个类,并在一个方法中返回该protected内部类的对象,
9
+ * 在返回的时候向上转型为第一个包中的接口的类型
10
+ */
11
+ public interface MyInterface {
12
+ void print ();
13
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses .exercise6 .package2 ;
2
+
3
+ import org .thinkinjava .innerclasses .exercise6 .package1 .MyInterface ;
4
+
5
+ public class MyInnerImpl {
6
+
7
+ protected class MyInner implements MyInterface {
8
+
9
+ public MyInner (){}
10
+
11
+ @ Override
12
+ public void print () {
13
+ System .out .println ("MyInner.print()" );
14
+ }
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .innerclasses .exercise6 .package3 ;
2
+
3
+ import org .thinkinjava .innerclasses .exercise6 .package1 .MyInterface ;
4
+ import org .thinkinjava .innerclasses .exercise6 .package2 .MyInnerImpl ;
5
+
6
+ public class MyExtendsImpl extends MyInnerImpl {
7
+
8
+
9
+ public MyInterface returnInner (){
10
+ return this .new MyInner ();
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .others ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ public class Learn1 {
6
+
7
+ /**
8
+ * 测试基本类型的==
9
+ */
10
+ @ Test
11
+ public void test01 (){
12
+ int a = 1 ;
13
+ long b = 1 ;
14
+ double c = 1 ;
15
+ System .out .println (a == b );
16
+ System .out .println (a == c );
17
+ }
18
+
19
+ @ Test
20
+ public void test02 (){
21
+ Integer a = 1 ;
22
+ Double b = 1.0 ;
23
+ Long c = 1L ;
24
+ System .out .println (a .equals (b ));
25
+ System .out .println (a .equals (c ));
26
+ }
27
+
28
+ @ Test
29
+ public void test03 (){
30
+ Integer a = 1 ;
31
+ Integer b = new Integer (1 );
32
+ Integer c = new Integer (1 );
33
+ System .out .println ("a.equals(b) : " + a .equals (b ));
34
+ System .out .println ("a == b : " + (a == b ));
35
+ System .out .println ("b.equals(c) : " + b .equals (c ));
36
+ System .out .println ("b == c : " + (b == c ));
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ package org .thinkinjava .others ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ class User {
6
+ private String name ;
7
+
8
+ public String getName () {
9
+ return name ;
10
+ }
11
+
12
+ public void setName (String name ) {
13
+ this .name = name ;
14
+ }
15
+ }
16
+
17
+ class Users {
18
+ private User user1 ;
19
+
20
+ private User user2 ;
21
+
22
+ public Users (){
23
+ user1 = new User ();
24
+ }
25
+ public User getUser1 () {
26
+ return user1 ;
27
+ }
28
+
29
+ public void setUser1 (User user1 ) {
30
+ this .user1 = user1 ;
31
+ }
32
+
33
+ public User getUser2 () {
34
+ return user2 ;
35
+ }
36
+
37
+ public void setUser2 (User user2 ) {
38
+ this .user2 = user2 ;
39
+ }
40
+ }
41
+ public class Learn2 {
42
+
43
+ @ Test
44
+ public void test01 (){
45
+ User user = new User ();
46
+ user .setName ("aaa" );
47
+
48
+ Users users = new Users ();
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments