Ray's playground

 

Type Information(Thinking in Java)

Shapes
 1 import java.util.*;
 2 
 3 abstract class Shape
 4 {
 5     void draw()
 6     {
 7         System.out.println(this + ".draw()");
 8     }
 9     abstract public String toString();
10 }
11 
12 class Circle extends Shape
13 {
14     public String toString()
15     {
16         return "Circle";
17     }
18 }
19 
20 class Square extends Shape
21 {
22     public String toString()
23     {
24         return "Square";
25     }
26 }
27 
28 class Triangle extends Shape
29 {
30     public String toString()
31     {
32         return "Triangle";
33     }
34 }
35 
36 public class Shapes
37 {
38     public static void main(String[] args)
39     {
40         List<Shape> shapeList = Arrays.asList(new Circle(), new Square(), new Triangle());
41         for(Shape shape : shapeList)
42         {
43             shape.draw();
44         }
45     }
46 }
47 

SweetShop
 1 class Candy
 2 {
 3     static 
 4     {
 5         System.out.println("Loading Candy");
 6     }
 7 }
 8 
 9 class Gum
10 {
11     static
12     {
13         System.out.println("Loading Gum");
14     }
15 }
16 
17 class Cookie
18 {
19     static
20     {
21         System.out.println("Loading Cookie");
22     }
23 }
24 
25 public class SweetShop 
26 {
27     public static void main(String[] args)
28     {
29         System.out.println("inside main");
30         new Candy();
31         System.out.println("After creating Candy");
32         try
33         {
34             Class.forName("Gum");
35         }
36         catch(ClassNotFoundException e)
37         {
38             System.out.println("Couldn't find Gum");
39         }
40         System.out.println("After Class.forName(\"Gum\")");
41         new Cookie();
42         System.out.println("After creating Cookie");
43     }
44 } 

 

ToyTest
 1 interface HasBatteries{}
 2 interface Waterproof{}
 3 interface Shoots{}
 4 
 5 class Toy
 6 {
 7     Toy(){}
 8     Toy(int i){}
 9 }
10 
11 class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots
12 {
13     FancyToy() { super(1); }
14 }
15 
16 public class ToyTest {
17     static void printInfo(Class cc)
18     {
19         System.out.println("Class name: " + cc.getName() +
20                 " is interface? [" + cc.isInterface() + "]");
21         System.out.println("Simple name: " + cc.getSimpleName());
22         System.out.println("Canonical name: " + cc.getCanonicalName());
23     }
24     
25     public static void main(String[] args)
26     {
27         Class c = null;
28         try
29         {
30             c = Class.forName("FancyToy");
31         }
32         catch(ClassNotFoundException e)
33         {
34             System.out.println("Cannot find FancyToy");
35             System.exit(1);
36         }
37         printInfo(c);
38         for(Class face : c.getInterfaces())
39         {
40             printInfo(face);
41         }
42         Class up = c.getSuperclass();
43         Object obj = null;
44         try
45         {
46             obj = up.newInstance();
47         }
48         catch(InstantiationException e)
49         {
50             System.out.println("Cannot instantiate");
51         }
52         catch(IllegalAccessException e)
53         {
54             System.out.println("Cannot access");
55             System.exit(1);
56         }
57         
58         printInfo(obj.getClass());
59     }
60 }

 

 

 

posted on 2010-03-14 17:39  Ray Z  阅读(333)  评论(0编辑  收藏  举报

导航