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

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

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

    VIRGIN FOREST OF JAVA
    不要埋頭苦干,要學習,學習,再學習。。。。。
    powered by R.Zeus
    ?

    Explore Java's static nested classes and inner classes


    by? David?Petersheim ?|? More from David?Petersheim ?|? Published: 8/12/05

    Starting with JDK 1.1, Java provided the ability to create nested classes. A nested class is defined inside another class. There are two types of nested classes: static nested classes and inner classes.

    A static nested class is declared inside another class with the static keyword or within a static context of that class. A static class has no access to instance-specific data.

    An inner class is a nonstatic class declared inside another class. It has access to all of its enclosing class's instance data, including private fields and methods. Inner classes may access static data but may not declare static members unless those members are compile time constants.

    Deciding which type of nested class to use depends on the data type your nested class needs to access. If you need access to instance data, you'll need an inner class. If you don't need access to instance data, a static nested class will suffice.

    The most common use of inner classes is as event handlers for GUI-based applications. These classes are usually declared anonymously and as needed for each component that requires an event handler. The advantage of using an inner class for event handling is that you can avoid large If/else statements to decide which component is being handled. Each component gets its own event handler, so each event handler knows implicitly the component for which it's working.

    The snippet below creates an anonymous inner class to handle the events created by an application's OK button.

    Button btn = new Button("Ok");
    btn.addActionListener(
    ????new ActionListener() {
    ????????public void actionPerformed(ActionEvent ae) {
    ????????????okClicked();
    ????????}
    ????}
    );

    The advantage of a static nested class is that it doesn't need an instance of the containing class to work. This can help you reduce the number of objects your application creates at runtime.

    The semantics for creating instances of nested classes can be confusing. Below is a simple class that defines a static nested class and an inner class. Pay special attention to the main method, where an instance of each instance class is created.

    // creating an instance of the enclosing class
    NestedClassTip nt = new NestedClassTip();


    // creating an instance of the inner class requires
    // a reference to an instance of the enclosing class
    NestedClassTip.NestedOne nco = nt.new NestedOne();


    // creating an instance of the static nested class
    // does not require an instance of the enclosing class
    NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();



    public class NestedClassTip {
    ????private String name = "instance name";
    ????private static String staticName = "static name";

    ????public static void main(String args[]) {
    ????????NestedClassTip nt = new NestedClassTip();

    ????????NestedClassTip.NestedOne nco = nt.new NestedOne();

    ????????NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();
    ????}

    ????class NestedOne {
    ????????NestedOne() {
    ????????????System.out.println(name);
    ????????????System.out.println(staticName);
    ????????}
    ????}

    ????static class NestedTwo {
    ????????NestedTwo() {
    ????????????System.out.println(staticName);
    ????????}
    ????}
    }

    Nested classes can be confusing, but once you understand their purpose and get used to the semantics, there isn't a lot to them. If you'd like to learn more about the details of nested classes, check out the Java Language Specification.

    ?

    posted on 2006-11-03 15:42 R.Zeus 閱讀(366) 評論(0)  編輯  收藏 所屬分類: J2SE
    主站蜘蛛池模板: 国产精品久久久久久亚洲影视| 日本高清免费不卡在线| 9420免费高清在线视频| 少妇无码一区二区三区免费| 免费精品无码AV片在线观看| 91黑丝国产线观看免费| 成人毛片免费视频| 亚洲美女高清一区二区三区| 亚洲免费在线观看| 久久精品国产亚洲AV麻豆不卡| 亚洲天天做日日做天天欢毛片| 亚洲a级片在线观看| 日本媚薬痉挛在线观看免费| 亚洲国产综合无码一区二区二三区| 国产亚洲午夜高清国产拍精品 | 国产精品免费观看调教网| 最近中文字幕完整版免费高清| a级毛片无码免费真人| 亚洲AV中文无码乱人伦在线视色| 亚洲午夜久久久久久噜噜噜| 亚洲成av人片在线看片| 免费人成动漫在线播放r18| 久久久高清日本道免费观看| 午夜免费福利在线观看| 国产偷国产偷亚洲清高动态图| 亚洲精品国产专区91在线| 偷自拍亚洲视频在线观看| 国产好大好硬好爽免费不卡| 免费无码不卡视频在线观看| 亚洲日韩激情无码一区| 日韩亚洲产在线观看| 波多野结衣免费一区视频| 日本免费一本天堂在线| 亚洲av日韩综合一区在线观看| 色综合久久精品亚洲国产| 一级特黄aa毛片免费观看| 国产亚洲色视频在线| 亚洲丁香婷婷综合久久| 亚洲第一网站免费视频| 亚洲亚洲人成综合网络| 一级特级女人18毛片免费视频|