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

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

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

    Scott@JAVA

    Java, 一杯濃濃的咖啡伴你到深夜

    A Glossary of Name Reuse

    From <<Java Puzzlers>>, Chapter 8. Classier Puzzlers


    A Glossary of Name Reuse

    Most of the puzzles in this chapter were based on name reuse. This section summarizes the various forms of name reuse.

    Overriding

    An instance method overrides all accessible instance methods with the same signature in superclasses [JLS 8.4.8.1], enabling dynamic dispatch; in other words, the VM chooses which overriding to invoke based on an instance's run-time type [JLS 15.12.4.4]. Overriding is fundamental to object-oriented programming and is the only form of name reuse that is not generally discouraged:

    class Base {
    public void f() { }
    }
    class Derived extends Base {
    public void f() { } // overrrides Base.f()
    }
    

    Hiding

    A field, static method, or member type hides all accessible fields, static methods, or member types, respectively, with the same name (or, for methods, signature) in supertypes. Hiding a member prevents it from being inherited [JLS 8.3, 8.4.8.2, 8.5]:

    class Base {
    public static void f() { }
    }
    class Derived extends Base {
    public static void f() { } // hides Base.f()
    }
    

    Overloading

    Methods in a class overload one another if they have the same name and different signatures. The overloaded method designated by an invocation is selected at compile time [JLS 8.4.9, 15.12.2]:

    class CircuitBreaker {
    public void f(int i)    { } // int overloading
    public void f(String s) { } // String overloading
    }
    

    Shadowing

    A variable, method, or type shadows all variables, methods, or types, respectively, with the same name in a textually enclosing scope. If an entity is shadowed, you cannot refer to it by its simple name; depending on the entity, you cannot refer to it at all [JLS 6.3.1]:

    class WhoKnows {
    static String sentence = "I don't know.";
    public static void main(String[] args) {
    String sentence = "I know!";   // shadows static field
    System.out.println(sentence);  // prints local variable
    }
    }
    

    Although shadowing is generally discouraged, one common idiom does involve shadowing. Constructors often reuse a field name from their class as a parameter name to pass the value of the named field. This idiom is not without risk, but most Java programmers have decided that the stylistic benefits outweigh the risks:

    class Belt {
    private final int size;
    public Belt(int size) { // Parameter shadows Belt.size
    this.size = size;
    }
    }
    

    Obscuring

    A variable obscures a type with the same name if both are in scope: If the name is used where variables and types are permitted, it refers to the variable. Similarly, a variable or a type can obscure a package. Obscuring is the only kind of name reuse where the two names are in different namespaces: variables, packages, methods, or types. If a type or a package is obscured, you cannot refer to it by its simple name except in a context where the syntax allows only a name from its namespace. Adhering to the naming conventions largely eliminates obscuring [JLS 6.3.2, 6.5]:

    public class Obscure {
    static String System; // Obscures type java.lang.System
    public static void main(String[] args) {
    // Next line won't compile: System refers to static field
    System.out.println("hello, obscure world!");
    }
    }
    

    posted on 2007-07-25 15:26 Scott@JAVA 閱讀(434) 評論(0)  編輯  收藏 所屬分類: Jave SE 6

    主站蜘蛛池模板: baoyu122.永久免费视频| 成人免费视频88| 亚洲首页国产精品丝袜| 国产无遮挡又黄又爽免费视频| 免费一级毛suv好看的国产网站 | 亚洲日韩中文字幕在线播放| 91禁漫免费进入| 美女扒开尿口给男人爽免费视频 | 久久久久亚洲精品无码蜜桃 | 亚洲综合视频在线观看| 白白国产永久免费视频| 两个人看的www高清免费视频| 亚洲AV无码久久久久网站蜜桃 | 一个人免费视频观看在线www| 91丁香亚洲综合社区| 亚洲综合色成在线播放| 国国内清清草原免费视频99 | a级毛片无码免费真人久久| 亚洲日韩国产欧美一区二区三区| 亚洲精品麻豆av| 久久不见久久见免费影院| 国产99视频精品免费视频76| 亚洲成a人片在线观看中文!!! | 久久久久亚洲AV综合波多野结衣| 久久精品人成免费| 爱情岛亚洲论坛在线观看 | 一级毛片**免费看试看20分钟| 亚洲av日韩av天堂影片精品| 四色在线精品免费观看| 最近免费中文字幕MV在线视频3| 亚洲欧美日韩自偷自拍| 亚洲第一精品电影网| 国产精品亚洲成在人线| 亚洲AV无码一区二区三区国产| 免费国产作爱视频网站| 久久久久成人精品免费播放动漫| a在线视频免费观看在线视频三区 a毛片成人免费全部播放 | 在线观看特色大片免费网站| 日韩精品无码免费视频| 亚洲精品动漫免费二区| 亚洲AV无码成人专区|