<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 303  文章 - 883  trackbacks - 0
    <2007年2月>
    28293031123
    45678910
    11121314151617
    18192021222324
    25262728123
    45678910

    歡迎光臨! 
    閑聊 QQ:1074961813

    隨筆分類(357)

    我管理的群

    公共blog

    • n維空間
    • Email : java3d@126.com 群 : 12999758

    參與管理的論壇

    好友的blog

    我的其他blog

    朋友的網站

    搜索

    •  

    最新評論

    Java Basics (contd.)

    1. Interfaces

    An interface declares a set of methods and constants, without actually providing an implementation for any of those methods.?A class is said to implement an interface if it provides definitions for all of the methods declared in the interface.

    Interfaces provide a way to prescribe the behavior that a class must have. In this sense, an interface bears some resemblance to an abstract class.?An abstract class may contain default implementations for some of its methods; it is an incomplete class that must be specialized by subclassing.?By constrast, an interface does not provide default implementations for any of the methods.?It is just a way of specifying the functions that a class should contain. There is no notion of specialization through function overriding.

    Some points to note about interfaces:

    A class may implement more than one interface, whereas it can only extend one parent class.

    An interface is treated as a reference type.

    Interfaces provide a mechanism for callbacks, rather like pointers to functions in C++.

    An interface can extend another interface.

    Here is an example of using an interface.

    import java.util.*;

    interface Collection {
    ??? final int MAXIMUM = 100;??????????? // An interface can only have constant data.
    ??? public void add(Object obj);
    ??? public void remove();
    ??? public void print();
    }

    class Stack implements Collection {?????????? // A last in first out (LIFO) process.
    ??? Vector mVector;

    ??? public Stack() {
    ??????? mVector = new Vector(0);??????????????? // Create an empty vector.
    ??? }

    ??? // This adds an element to the top of the stack.
    ??? public void add(Object obj) {
    ??????? if (mVector.size() < MAXIMUM)???? // Restrict the size of the Stack.
    ??????????? mVector.insertElementAt(obj, 0);
    ??????? else
    ??????????? System.out.println("Reached maximum size");
    ??? }

    ??? // This removes an element from the top of the stack.
    ??? public void remove() {
    ??????? mVector.removeElementAt(0);
    ??? }

    ??? // This prints out the stack in order from top to bottom.
    ??? public void print() {
    ??????? System.out.println("Printing out the stack");
    ??????? for (int i = 0; i < mVector.size(); i++)
    ??????????? System.out.println(mVector.elementAt(i));
    ??? }
    }

    class Queue implements Collection {?????????? // A first in first out (FIFO) process.
    ??? Vector mVector;

    ??? public Queue() {
    ??????? mVector = new Vector(0);???????????????? // Create an empty vector.
    ??? }

    ??? // This adds an element to the bottom of the queue.
    ??? public void add(Object obj) {
    ??????? if (mVector.size() < MAXIMUM)????? // Restrict the size of the Queue.
    ??????????? mVector.addElement(obj);
    ??????? else
    ??????????? System.out.println("Reached maximum size");
    ??? }

    ??? // This removes an element from the top of the queue.
    ??? public void remove() {
    ??????? mVector.removeElementAt(0);
    ??? }

    ??? // This prints out the queue in order from top to bottom.
    ??? public void print() {
    ??????? System.out.println("Printing out the queue");
    ??????? for (int i = 0; i < mVector.size(); i++)
    ??????????? System.out.println(mVector.elementAt(i));
    ??? }
    }
    ?

    class Main {
    ??? public static void main(String[] args) {

    ??????? // Create a stack and add some objects to it.? The function CreateSomeObjects takes a
    ??????? // reference to the Collection interface as an argument, so it does not need to know anything
    ??????? // about the Stack class except that it supplies all the methods that the Collection interface
    ??????? // requires.? This is an example of using callbacks.
    ??????? Stack s = new Stack();
    ??????? CreateSomeObjects(s);

    ??????? // Remove an element from the stack and then print it out.
    ??????? s.remove();
    ??????? s.print();???????? // This will print out the elements 3,7,5.
    ?

    ??????? // Create a queue and add some objects to it.
    ??????? Queue q = new Queue();
    ??????? CreateSomeObjects(q);

    ??????? // Remove an element from the queue and then print it out.
    ??????? q.remove();
    ??????? q.print();???????? // This will print out the elements 7,3,4.
    ??? }
    ?

    ??? // Create some objects and add them to a collection.? Class Integer allows us to create integer
    ??? // objects from the corresponding primitive type, int.
    ??? public static void CreateSomeObjects(Collection c) {
    ??????? c.add(new Integer(5));
    ??????? c.add(new Integer(7));
    ??????? c.add(new Integer(3));
    ??????? c.add(new Integer(4));
    ??? }
    }


    2. Exceptions and Error Handling

    What happens when a program encounters a run-time error??Should it exit immediately or should it try to recover??The behavior that is desired may vary depending on how serious the error is.?A "file not found" error may not be a reason to terminate the program, whereas an "out of memory error" may.?One way to keep track of errors is to return an error code from each function.?Exceptions provide an alternative way to handle errors.

    The basic idea behind exceptions is as follows.?Any method with the potential to produce a remediable error should declare the type of error that it can produce using the throws keyword.?The basic remediable error type is class Exception, but one may be more specific about the type of exception that can be thrown e.g. IOException refers to an exception thrown during an input or output operation. When an exception occurs, we use the throw keyword to actually create the Exception object and exit the function.

    Code that has the potential to produce an exception should be placed within the try block of a try-catch statement. If the code succeeds, then control passes to the next statement following the try-catch statement. If the code within the try block fails, then the code within the catch block is executed.?The following example illustrates this.
    ?

    class LetterTest {
    ????? char readLetter() throws Exception {??????? // Indicates type of exception thrown.
    ???????????? int k;

    ???????????? k = System.in.read();
    ???????????? if (k < 'A' || k > 'z') {
    ?????????????????? throw new Exception();????????????????? // Throw an exception.
    ???????????? }

    ???????????? return (char)k;
    ????? }

    ????? public static void main(String[] args) {
    ???????????? LetterTest a = new LetterTest();

    ???????????? try {
    ??????????????????? char c = a.readLetter();
    ??????????????????? String str;
    ??????????????????? str = "Successfully read letter " + c;
    ??????????????????? System.out.println(str);
    ????????????? }
    ???????????? catch (Exception e) {?????????????????????????? // Handle the exception.
    ??????????????????? System.out.println("Failed to read letter.");
    ???????????? }
    ????? }
    }
    ?

    Note: in addition to the Exception class, Java? also provides an Error class, which is reserved for those kinds of problems that a reasonable program should not try to catch.



    地震讓大伙知道:居安思危,才是生存之道。
    posted on 2007-02-25 15:44 小尋 閱讀(215) 評論(0)  編輯  收藏 所屬分類: j2se/j2ee/j2me
    主站蜘蛛池模板: 亚洲AV永久无码天堂影院| 亚洲AV永久纯肉无码精品动漫| 日韩亚洲Av人人夜夜澡人人爽 | 中文字幕亚洲乱码熟女一区二区 | 亚洲同性男gay网站在线观看| 日韩内射激情视频在线播放免费| 亚洲国产成人久久精品影视| 日韩午夜理论免费TV影院| 亚洲妓女综合网99| 在线看片免费不卡人成视频| 亚洲一线产区二线产区区| 四虎www免费人成| 特级aaaaaaaaa毛片免费视频| 亚洲欧洲久久久精品| 99久久国产精品免费一区二区| 亚洲ⅴ国产v天堂a无码二区| 日韩精品极品视频在线观看免费 | 亚洲人成网www| 精品国产无限资源免费观看| 中国亚洲呦女专区| 亚洲国产精品日韩专区AV| 99在线视频免费观看| 亚洲第一二三四区| 成年18网站免费视频网站| 无人视频在线观看免费播放影院| 亚洲熟妇无码AV在线播放| 在线看片免费人成视久网| 亚洲色大成网站www永久男同 | 色噜噜狠狠色综合免费视频| 亚洲综合国产一区二区三区| 最近中文字幕无免费| 欧洲亚洲国产精华液| 久久精品亚洲视频| 午夜高清免费在线观看| 巨胸喷奶水视频www免费视频| 亚洲视频在线观看不卡| 永久免费看bbb| 香蕉免费一区二区三区| 亚洲高清毛片一区二区| 亚洲中文字幕无码一久久区| 人妻视频一区二区三区免费|