隨筆-8  評(píng)論-0  文章-0  trackbacks-0
            2005年8月25日
           
          public class NullPointerException
          extends RuntimeException

          當(dāng)一個(gè)應(yīng)用在需要一個(gè)對(duì)象的地方試圖使用 null 時(shí)拋出。它們包括:

          • 調(diào)用一個(gè) null 對(duì)象的實(shí)例方法。
          • 訪(fǎng)問(wèn)或修改一個(gè) null 對(duì)象的域。
          • null 作為一個(gè)數(shù)組,使用它的長(zhǎng)度。
          • null 作為一個(gè)數(shù)組,訪(fǎng)問(wèn)或修改它的插口。
          • null 作為一個(gè) Throwable 值拋出。

          應(yīng)用應(yīng)該拋出該類(lèi)的實(shí)例,指示其它對(duì) null 對(duì)象的非法使用。






          構(gòu)造子索引

          NullPointerException()
          構(gòu)造一個(gè)沒(méi)有詳細(xì)消息的 NullPointerException
          NullPointerException(String)
          構(gòu)造一個(gè)具有指定詳細(xì)消息的 NullPointerException




          構(gòu)造子

          NullPointerException
           public NullPointerException()
          
          構(gòu)造一個(gè)沒(méi)有詳細(xì)消息的 NullPointerException

          NullPointerException
           public NullPointerException(String s)
          
          構(gòu)造一個(gè)具有指定詳細(xì)消息的 NullPointerException

          參數(shù):
          s - 詳細(xì)消息。

          posted @ 2005-08-25 13:43 hegen 閱讀(1027) | 評(píng)論 (0)編輯 收藏
           Single inheritance precludes some useful and correct designs.The problems of multiple inheritance arise from multiple inheritance of implememtion, but in many cases multiple inheritance is used to inhert a number of abstract contracts and perhaps one concrete implementtation.Providing a means to inherit an abstract contract without inheriting an implementation allows the typing benefits of multiple inheritance without the problems of multiple implementation inheritance.The inheritance of an abstract contract is termed interface inhertiance.
          posted @ 2005-08-25 02:14 hegen 閱讀(190) | 評(píng)論 (0)編輯 收藏
            2005年8月18日
           看完了程功的故事后,感到只有穩(wěn)扎穩(wěn)打才是真,只有努力才能成功,我是笨鳥(niǎo),笨鳥(niǎo)只有以勤補(bǔ)拙,笨鳥(niǎo)是要先飛的.
          posted @ 2005-08-18 00:44 hegen 閱讀(178) | 評(píng)論 (0)編輯 收藏
            2005年8月11日
          Garbage Collection
          State the behavior that is guaranteed by the garbage collection system and write code that explicitly makes objects eligible for collection.
          1.    Garbage collection is a mechanism for reclaiming memory from objects that are no longer in use, and making the memory available for new objects.
          2.    An object being no longer in use means that it can’t be referenced by any ‘a(chǎn)ctive’ part of the program.
          3.    Garbage collection runs in a low priority thread. It may kick in when memory is too low. No guarantee.
          4.    It’s not possible to force garbage collection. Invoking System.gc may start garbage collection process.
          5.    There are no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. gc might not even be run if the program execution does not warrant it. Thus any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the OS or by other means.
          6.    There are also no guarantees on the order in which the objects will be garbage collected or on the order in which the finalizers are called.
          7.    Circular references do not prevent objects from being garbage collected.
          8.    We can set the reference variables to null, hinting the gc to garbage collect the objects referred by the variables. Even if we do that, the object may not be gc-ed if it’s attached to a listener. (Typical in case of AWT components) Remember to remove the listener first.
          9.    All objects have a finalize method. It is inherited from the Object class.
          10.    finalize method is used to release system resources other than memory. (such as file handles and network connections) The order in which finalize methods are called may not reflect the order in which objects are created. Don’t rely on it. This is the signature of the finalize method.
          protected void finalize() throws Throwable { }
          In the descendents this method can be protected or public. Descendents can restrict the exception list that can be thrown by this method.
          11.    finalize is called only once for an object. If any exception is thrown in finalize, the object is still eligible for garbage collection (at the discretion of gc)
          12.    gc keeps track of unreachable objects and garbage-collects them, but an unreachable object can become reachable again by letting know other objects of its existence from its finalize method (when called by gc). This ‘resurrection’ can be done only once, since finalize is called only one for an object.
          13.    finalize can be called explicitly, but it does not garbage collect the object.
          14.    finalize can be overloaded, but only the method with original finalize signature will be called by gc.
          15.    finalize is not implicitly chained. A finalize method in sub-class should call finalize in super class explicitly as its last action for proper functioning. But compiler doesn’t enforce this check.
          16.    System.runFinalization can be used to run the finalizers (which have not been executed before) for the objects eligible for garbage collection.
          17.    Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.  
          18.    Java uses a "mark sweep garbage collection algorithm, which traverses all the object references, marking any objects that are referred to and then garbage collecting any objects that are unmarked.
          19.    Java allows you to add a finalize() method to any class. The finalize() method will be called before the garbage collector sweeps away the object. In practice, do not rely on the finalize method for recycling any resources that are in short supply - you simply cannot know when this method will be called.


          In the exam point of view :
          ??You must be able to identify when an object is available for gc - you have either set it to null or you
          have "redirected" the variable that was originally referring to it, so that it now refers to a different
          object.
          ??if you have a reference to an object say, A and then you pass A as an argument to some constructor -
          new obj(A); - then even if you null your reference - A=null; - you can't say that A is available for
          gc. So just follow the references and when they drop to zero you know its eligible/available for gc,
          not that it will happen.


          I can not full understand these statements which are above.



          eg,
          1. obj = new Jo();
          2. obj.doSomething();
          3. obj = new Jo(); //Same as obj=null;
          4. obj.doSomething();

          Object a = new Object();
          Object a=null; //Now the object created in 1st line is available for gc
          Object a=new Object();
          a = new Object(); //same.
          // Now original object created in line 1 is available for gc and a new
          object is now out there referenced by "a".

          Aclass a = new Aclass(); // Object 1
          Aclass b= new Aclass(); // Object 2
          Aclass c = new Aclass(); // Object 3
          a=b; // now we have no valid object reference to object "a" and it will be
          // garbage collected sometime after this statement. But when?......
          a=c;
          c=null; // no garbage collection will be eligible since
          // "a" still refers to Object 3
          a=null; // now object "c" is eligible for gc since it always had a valid reference.
          // Should "b" go out of scope; then we would possibly have eligibility for gc.
          // there might still be other references to object "b" preventing the collection.
          posted @ 2005-08-11 13:46 hegen 閱讀(247) | 評(píng)論 (0)編輯 收藏
            2005年8月10日

          Text Color Codes

          In order to change text colors, you will need two things:

          1. A command to change the text.
          2. A color (hex) code.


          Changing Full-Page Text ColorsYou have the ability to change full-page text colors over four levels:

          <TEXT="######"> -- This denotes the full-page text color.

          <LINK="######"> -- This denotes the color of the links on your page.

          <ALINK="######"> -- This denotes the color the link will flash when clicked upon.

          <VLINK="######"> -- This denotes the colors of the links after they have been visited.

          These commands come right after the <TITLE> commands. Again, in that position they affect everything on the page. Also... place them all together inside the same command along with any background commands. Something like this:

          < BODY BGCOLOR="######" TEXT="######" LINK="######" VLINK="######">

          Please note: When you write these codes, you can write them with a # sign in front of the hex code or not. It used to be that that was required, but not any more. I still use it just because I started that way. You may want to just go with the six-digit code. Also make sure to place a space between each command and be sure to enclose it in quotation marks, like so:

          <VLINK="#FFFFFF">

          問(wèn)題:
            上面的那幾個(gè)東西還沒(méi)有搞懂.不知道什么用?


          Changing Specific Word ColorBut I only want to change one word's color!

          You'll use a color (hex) code to do the trick. Follow this formula:

          <FONT COLOR="######">text text text text text</FONT>

          It's a pain in the you-know-where, but it gets the job done. It works with all H commands and text size commands. Basically, if it's text, it will work.


          posted @ 2005-08-10 20:48 hegen 閱讀(331) | 評(píng)論 (0)編輯 收藏

          Please note:
          "Aqua" and "Cyan" produce the same color: 00FFFF
          "Fuchsia" and "Magenta" produce the same color: FF00FF

          Aliceblue
          F0F8FF
          Antiquewhite
          FAEBD7
          Aqua
          00FFFF
          Aquamarine
          7FFFD4
          Azure
          F0FFFF
          Beige
          F5F5DC
          Bisque
          FFE4C4
          Black
          000000
          Blanchedalmond
          FFEBCD
          Blue
          0000FF
          Blueviolet
          8A2BE2
          Brown
          A52A2A
          Burlywood
          DEB887
          Cadetblue
          5F9EA0
          Chartreuse
          7FFF00
          Chocolate
          D2691E
          Coral
          FF7F50
          Cornflowerblue
          6495ED
          Cornsilk
          FFF8DC
          Crimson
          DC143C
          Cyan
          00FFFF
          Darkblue
          00008B
          Darkcyan
          008B8B
          Darkgoldenrod
          B8860B
          Darkgray
          A9A9A9
          Darkgreen
          006400
          Darkkhaki
          BDB76B
          Darkmagenta
          8B008B
          Darkolivegreen
          556B2F
          Darkorange
          FF8C00
          Darkorchid
          9932CC
          Darkred
          8B0000
          Darksalmon
          E9967A
          Darkseagreen
          8FBC8F
          Darkslateblue
          483D8B
          Darkslategray
          2F4F4F
          Darkturquoise
          00CED1
          Darkviolet
          9400D3
          deeppink
          FF1493
          Deepskyblue
          00BFFF
          Dimgray
          696969
          Dodgerblue
          1E90FF
          Firebrick
          B22222
          Floralwhite
          FFFAF0
          Forestgreen
          228B22
          Fuchsia
          FF00FF
          Gainsboro
          DCDCDC
          Ghostwhite
          F8F8FF
          Gold
          FFD700
          Goldenrod
          DAA520
          Gray
          808080
          Green
          008000
          Greenyellow
          ADFF2F
          Honeydew
          F0FFF0
          Hotpink
          FF69B4
          Indianred
          CD5C5C
          Indigo
          4B0082
          Ivory
          FFFFF0
          Khaki
          F0E68C
          Lavender
          E6E6FA
          Lavenderblush
          FFF0F5
          Lawngreen
          7CFC00
          Lemonchiffon
          FFFACD
          Lightblue
          ADD8E6
          Lightcoral
          F08080
          Lightcyan
          E0FFFF
          Lightgoldenrodyellow
          FAFAD2
          Lightgreen
          90EE90
          Lightgrey
          D3D3D3
          Lightpink
          FFB6C1
          Lightsalmon
          FFA07A
          Lightseagreen
          20B2AA
          Lightskyblue
          87CEFA
          Lightslategray
          778899
          Lightsteelblue
          B0C4DE
          Lightyellow
          FFFFE0
          Lime
          00FF00
          Limegreen
          32CD32
          Linen
          FAF0E6
          Magenta
          FF00FF
          Maroon
          800000
          Mediumauqamarine
          66CDAA
          Mediumblue
          0000CD
          Mediumorchid
          BA55D3
          Mediumpurple
          9370D8
          Mediumseagreen
          3CB371
          Mediumslateblue
          7B68EE
          Mediumspringgreen
          00FA9A
          Mediumturquoise
          48D1CC
          Mediumvioletred
          C71585
          Midnightblue
          191970
          Mintcream
          F5FFFA
          Mistyrose
          FFE4E1
          Moccasin
          FFE4B5
          Navajowhite
          FFDEAD
          Navy
          000080
          Oldlace
          FDF5E6
          Olive
          808000
          Olivedrab
          688E23
          Orange
          FFA500
          Orangered
          FF4500
          Orchid
          DA70D6
          Palegoldenrod
          EEE8AA
          Palegreen
          98FB98
          Paleturquoise
          AFEEEE
          Palevioletred
          D87093
          Papayawhip
          FFEFD5
          Peachpuff
          FFDAB9
          Peru
          CD853F
          Pink
          FFC0CB
          Plum
          DDA0DD
          Powderblue
          B0E0E6
          Purple
          800080
          Red
          FF0000
          Rosybrown
          BC8F8F
          Royalblue
          4169E1
          Saddlebrown
          8B4513
          Salmon
          FA8072
          Sandybrown
          F4A460
          Seagreen
          2E8B57
          Seashell
          FFF5EE
          Sienna
          A0522D
          Silver
          C0C0C0
          Skyblue
          87CEEB
          Slateblue
          6A5ACD
          Slategray
          708090
          Snow
          FFFAFA
          Springgreen
          00FF7F
          Steelblue
          4682B4
          Tan
          D2B48C
          Teal
          008080
          Thistle
          D8BFD8
          Tomato
          FF6347
          Turquoise
          40E0D0
          Violet
          EE82EE
          Wheat
          F5DEB3
          White
          FFFFFF
          Whitesmoke
          F5F5F5
          this color model is about html
          posted @ 2005-08-10 20:37 hegen 閱讀(229) | 評(píng)論 (0)編輯 收藏

          實(shí)例形式的變壓器模式的類(lèi)圖定義如下。


          圖2. 實(shí)例變壓器模式的類(lèi)圖定義

          在圖1可以看出,模式所涉及的成員有:
          • 目標(biāo)(Target)。這就是我們所期待得到的接口。目標(biāo)可以是實(shí)的或抽象的類(lèi)。

          • 源(Adaptee)。現(xiàn)有需要適配的接口。

          • 變壓器(Adapter)。變壓器類(lèi)是本模式的核心。變壓器把源接口轉(zhuǎn)換成目標(biāo)接口。 顯然,這一角色必須是實(shí)類(lèi)。

            本模式的示范代碼如下:

          package com.javapatterns.adapter;
          public interface Target {
              /**
               * Class Adaptee contains operation sampleOperation1.
               */
              void sampleOperation1();
              /**
               * Class Adaptee doesn't contain operation sampleOperation2.
               */
              void sampleOperation2();
          }
          
          代碼清單4. Target的源代碼。

          package com.javapatterns.adapter;
          public class Adapter implements Target {
          public Adapter(Adaptee adaptee){
                  super();
                  this.adaptee = adaptee;
              }
              public void sampleOperation1(){
                  adaptee.sampleOperation1();
              }
              public void sampleOperation2(){
                  // Write your code here
              }
              private Adaptee adaptee;
          }
          
            代碼清單5. Adapter的源代碼。

          package com.javapatterns.adapter;
          public class Adaptee {
              public void sampleOperation1(){}
          }
          
          代碼清單6. Adaptee的源代碼。

            實(shí)例形式的變壓器模式的效果

            第一、 一個(gè)變壓器可以把多種不同的源適配到同一個(gè)目標(biāo)。換言之,同一個(gè)變壓器可以把源類(lèi)和它的子類(lèi)都適配到目標(biāo)接口。

            第二、 與類(lèi)形式的變壓器模式相比,要想置換源類(lèi)的方法就不容易。如果一定要置換掉源類(lèi)的一個(gè)或多個(gè)方法,就只好先做一個(gè)源類(lèi)的子類(lèi), 將源類(lèi)的方法置換掉,然后再把源類(lèi)的子類(lèi)當(dāng)作真正的源進(jìn)行適配。

            第三、 雖然要想置換源類(lèi)的方法不容易,但是要想增加一些新的方法則方便得很。 而且新增加的方法同時(shí)適用于所有的源。


          利用變壓器模式指方為圓

            中國(guó)古代有趙高指鹿為馬的故事。鹿與馬有很多相似之處,沒(méi)見(jiàn)過(guò)的人本就分辨不清,指一指可能沒(méi)什么大不了的。 指方為圓是否太過(guò)?非也。本例就是要指方為圓,需要的只是變壓器模式這個(gè)魔術(shù)手指(Magic Finger)。

            變壓器模式在本例子的類(lèi)圖如下。


          圖6. 指方為圓的變壓器模式類(lèi)圖

          package com.javapatterns.adapter.cube2ball;
          public class Cube
          {
              public Cube(double width)
              {
                  this.width = width;
              }
              public double calculateVolume()
              {
               return width * width * width;
              }
              public double calculateFaceArea()
              {
                  return width * width;
              }
              public double getWidth()
              {
                  return this.width;
              }
              public void setWidth(double width)
              {
                  this.width = width;
              }
              private double width;
          }
          
          代碼清單8. Cube類(lèi)的源代碼。。

          package com.javapatterns.adapter.cube2ball;
          public interface BallIF
          {
              double calculateArea();
              double calculateVolume();
              double getRadius();
              void setRadius(double radius);
          }
          
          代碼清單9. BallIF接口的源代碼。

          package com.javapatterns.adapter.cube2ball;
          public class MagicFinger implements BallIF
          {
              public MagicFinger(Cube adaptee)
              {
                  super();
                  this.adaptee = adaptee;
                  radius = adaptee.getWidth();
              }
              public double calculateArea()
              {
                  return PI * 4.0D * ( radius * radius );
              }
              public double calculateVolume()
              {
                  return PI * 4.0D/3.0D * ( radius * radius * radius );
              }
              public double getRadius()
              {
               return radius;
              }
              public void setRadius(double radius)
              {
               this.radius = radius;
              }
              private double radius = 0;
              private static final double PI = 3.14D;
              private Cube adaptee;
          }
          
          代碼清單10. MagicFinger類(lèi)的源代碼。

            如果讀者還記得中學(xué)的數(shù)學(xué)的話(huà),應(yīng)該可以看出,我們的指方為圓系統(tǒng)其實(shí)還是有道理的。它接受一個(gè)正方體, 返還此正方體的內(nèi)切球,也就是能放進(jìn)此正方體的最大的球。

            顯然,本例子里,我們使用的是實(shí)例形式的變壓器模式。這樣做的好處是,如果一旦我們決定不僅要支持正方體, 而且要支持四面體等多面體,我們可以使用同一個(gè)MagicFinger類(lèi),而不必針對(duì)每一個(gè)多面體都建立一個(gè)MagicFinger類(lèi)。 這樣也比較符合“魔術(shù)手指”這個(gè)名字。
          posted @ 2005-08-10 18:40 hegen 閱讀(282) | 評(píng)論 (0)編輯 收藏
            2005年8月2日
          要用到j(luò)ava.io.File中的一些方法
          比如isFile
           public boolean isFile()
          
          測(cè)試當(dāng)前 File 對(duì)象表示的文件是否是一個(gè)“普通”文件。

          如果一個(gè)文件不是一個(gè)路徑且滿(mǎn)足其它系統(tǒng)的標(biāo)準(zhǔn),則它是一個(gè)“普通”文件。由 Java 的一個(gè)應(yīng)用程序創(chuàng)建的任何非路徑文件肯定是一個(gè)“普通”文件。

          返回值:
          如果當(dāng)前對(duì)象指定的文件存在且是一個(gè)“普通”文件則為 true;否則為 false
          拋出: SecurityException
          如果有一個(gè)安全管理器,則用當(dāng)前 File 的路徑名調(diào)用 checkRead 方法,查看是否允許此應(yīng)用程序讀該文件。
          參見(jiàn):
          getPath, checkRead


          File
           public File(File dir,
                            String name)
          
          創(chuàng)建一個(gè) File 實(shí)例,表示指定路徑指定名稱(chēng)的文件。

          如果路徑參數(shù)為 null, 則結(jié)果 File 實(shí)例表示在當(dāng)前路徑(與系統(tǒng)有關(guān))下的一個(gè)文件,它的路徑名是 name 參數(shù)。否則, File 實(shí)例表示一個(gè)文件,它的路徑名是路徑參數(shù)(dir)給定的路徑名,后跟分隔符和 name 參數(shù)。

          參數(shù):
          dir - 路徑。
          name - 文件路徑名。
          參見(jiàn):
          getPath, separator

          下面是這個(gè)程序的代碼:
          import java.io.*;
          import java.lang.*;
          public class Example20_4
          {
           public static void Traverse(File dir){
            System.out.println(dir.toString());
               String fileName[]=dir.list();
            for(int i=0;i<fileName.length;i++){
             System.out.println(fileName[i]);
             File dir1=new File(dir,fileName[i]);
             //String str=dir1.getPath();
             //File dir2=new File(str);
             if(dir1.isFile())
              continue;
             Traverse(dir1);
            }
            System.out.println();
           }
           public static void main(String [] args){
            File dir=new File(args[0]);
            Traverse(dir);
           }
          }



          posted @ 2005-08-02 17:29 hegen 閱讀(388) | 評(píng)論 (0)編輯 收藏
          僅列出標(biāo)題  
          主站蜘蛛池模板: 景洪市| 南郑县| 和田县| 岳池县| 游戏| 瑞昌市| 拜泉县| 新津县| 大丰市| 永善县| 安平县| 登封市| 大冶市| 新和县| 广河县| 林口县| 沈阳市| 阿拉善右旗| 阿克苏市| 区。| 景泰县| 东光县| 英吉沙县| 河津市| 西充县| 小金县| 同江市| 会同县| 开化县| 大埔县| 莫力| 延吉市| 玉龙| 渭源县| 逊克县| 九江县| 石门县| 咸阳市| 乐东| 乳源| 峨边|