频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Overloading overriding runtime type and object orientation (2)

来源: 作者: 出处:巧巧读书 2006-10-03 进入讨论组
访问地址 http://www.qqread.com/jsp/b227573.html
  Objective 3)
Write code to construct instances of any concrete class including normal top level classes inner classes static inner classes and anonymous inner classes.
Inner Classes
·    A class can be declared in any scope. Classes defined inside of other classes are known as nested classes. There are four categories of nested classes.
Top-level nested classes / interfaces
·    Declared as a class member with static modifier.
·    Just like other static features of a class. Can be accessed / instantiated without an instance of the outer class. Can access only static members of outer class. Can’t access non-static instance variables or methods.
·    Very much like any-other package level class / interface. Provide an extension to packaging by the modified naming scheme at the top level.
·    Classes can declare both static and non-static members.
·    Any accessibility modifier can be specified.
·    Nested interfaces are implicitly static (static modifier also can be specified). They can have any accessibility modifier. There are no non-static inner, local or anonymous interfaces.
Non-static inner classes
·    Declared as a class member without static.
·    An instance of a non-static inner class can exist only with an instance of its enclosing class. So it always has to be created within a context of an outer instance.
                Outer.Inner i = new Outer().new Inner();
·    Just like other non-static features of a class. Can access all the features (even private) of the enclosing outer class. Have an implicit reference to the enclosing instance.
·    Cannot have any static members.
·    Can have any access modifier.
Local classes
·    Defined inside a block (could be a method, a constructor, a local block, a static initializer or an instance initializer). Cannot be specified with static modifier.
·    Cannot have any access modifier (since they are effectively local to the block)
·    Cannot declare any static members.(Even declared in a static context)
·    Can access all the features of the enclosing class (because they are defined inside the method of the class) but can access only final variables defined inside the method (including method arguments). This is because the class can outlive the method, but the method local variables will go out of scope – in case of final variables, compiler makes a copy of those variables to be used by the class. (New meaning for final)
·    Since the names of local classes are not visible outside the local context, references of these classes cannot be declared outside. So their functionality could be accessed only via super-class references (either interfaces or classes). Objects of those class types are created inside methods and returned as super-class type references to the outside world. This is the reason that they can only access final variables within the local block. That way, the value of the variable can be always made available to the objects returned from the local context to outside world.
·    Cannot be specified with static modifier. But if they are declared inside a static context such as a static method or a static initializer, they become static classes. They can only access static members of the enclosing class and local final variables. But this doesn’t mean they cannot access any non-static features inherited from super classes. These features are their own, obtained via the inheritance hierarchy. They can be accessed normally with ‘this’ or ‘super’.
Anonymous classes
·    Anonymous classes are defined where they are constructed. They can be created wherever a reference expression can be used.
·    An anonymous class is never abstract . An anonymous class is always an inner class; it is never static . An anonymous class is always implicitly final.
·    Anonymous classes cannot have explicit constructors. Instance initializers can be used to achieve the functionality of a constructor.
·    Anonymous classes can implement an interface (implicit extension of Object) or explicitly extend a class. Cannot do both.
Syntax: new interfacename() { } or new classname(can take arg.) { }
·    Keywords implements and extends are not used in anonymous classes.
·    Abstract classes can be specified in the creation of an anonymous class. The new class is a concrete class, which automatically extends the abstract class.
·    Discussion for local classes on static/non-static context, accessing enclosing variables, and declaring static variables also holds good for anonymous classes. In other words, anonymous classes cannot be specified with static, but based on the context, they could become static classes. In any case, anonymous classes are not allowed to declare static members. Based on the context, non-static/static features of outer classes are available to anonymous classes. Local final variables are always available to them.
·    E.g.
   btn.addActionListener(
     new ActionListener() {
       void ActionPerformed() {System.out.println(s);}
     }
   )


·    One enclosing class can have multiple instances of inner classes.
·    Inner classes can have synchronous methods. But calling those methods obtains the lock for inner object only, not the outer object.  If you need to synchronize an inner class method based on outer object, outer object lock must be obtained explicitly. Locks on inner object and outer object are independent.
·    Nested classes can extend any class or can implement any interface. No restrictions.
·    All nested classes (except anonymous classes) can be abstract or final.
·    Classes can be nested to any depth. Top-level static classes can be nested only within other static top-level classes or interfaces. Deeply nested classes also have access to all variables of the outer-most enclosing class (as well the immediate enclosing class’s)
·    Member inner classes can be forward referenced. Local inner classes cannot be.(?)
·    Outer class variables are accessible within the inner class, but they are not inherited. They don’t become members of the inner class. This is different from inheritance. (Outer class cannot be referred using ‘super’, and outer class variables cannot be accessed using ‘this’)
·    An inner class variable can shadow an outer class variable. If the inner class is sub-classed within the same outer class, the variable has to be qualified explicitly in the sub-class. To fully qualify the variable, use (classname.this.variablename). If we don’t correctly qualify the variable, a compiler error will occur. (Note that this does not happen in multiple levels of inheritance where an upper-most super-class’s variable is silently shadowed by the most recent super-class variabl进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章