`
leonzhx
  • 浏览: 808903 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1)  It’s possible to place a class definition within another class definition. This is called an inner class.

 

2) If you want to make an object of the inner class anywhere except from within a method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName.

 

3) When you create an inner class, an object of that inner class has a link to the enclosing object that made it, and so it can access the members of that enclosing object—without any special qualifications. In addition, inner classes have access rights to all the elements in the enclosing class. And the enclosing class can also access all members of an inner class.

 

4) The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that reference is used to select that member. Fortunately, the compiler takes care of all these details for you, but now you can see that an object of an inner class can be created only in association with an object of the enclosing class (when the inner class is non-static). Construction of the inner-class object requires the reference to the object of the enclosing class, and the compiler will complain if it cannot access that reference. Most of the time this occurs without any intervention on the part of the programmer.

 

5) If you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this.(OuterClassName.this) The resulting reference is automatically the correct type, which is known and checked at compile time.

 

6) Sometimes you want to tell some other object to create an object of one of its inner classes. To do this you must provide a reference to the other outer-class object in the new expression, using the OuterCalssObj.new InnerClassName() syntax.

 

7) If you make a nested class (a static inner class), then it doesn’t need a reference to the outer-class object.

 

8) Inner classes can be created within a method or even an arbitrary scope. There are two reasons for doing this:

    a. You’re implementing an interface of some kind so that you can create and return a reference.
    b. You’re solving a complicated problem and you want to create a class to aid in your solution, but you don’t want it publicly available.

 

9) A class within the scope of a method or an arbitrary scode such as if scope (instead of the scope of another class) is called a local inner class. So it can only be accessed within that method or that scope.

 

10) An anonymous inner class is defined at the time you create an instance of the base class like :

new BaseClass() { //anonymous inner class definition} ;

The semicolon at the end of the anonymous inner class doesn’t mark the end of the class body. Instead, it marks the end of the expression that happens to contain the anonymous class. Thus, it’s identical to the use of the semicolon everywhere else.

 

11) If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class ( in its defining scope or method), the compiler requires that the argument reference be final. That also apply to local inner class.

 

12) With instance initialization, you can, in effect, create a constructor for an anonymous inner class.

 

13) If you don’t need a connection between the inner class object and the outer class object, then you can make the inner class static. This is commonly called a nested class.

 

14) A nested class means:
    a. You don’t need an outer class object in order to create an object of a nested class.
    b. You can’t access a non-static outer class object from an object of a nested class.

 

15) Nested classes are different from ordinary inner classes in another way, as well. static fields and methods in ordinary classes can only be at the outer level of a class, so ordinary inner classes cannot have static methods, static fields, nested classes or interfaces. However, nested classes can have all of these. It can be initialized by : new OuterClassName.InnerClassName();

 

16) Normally, you can’t put any code inside an interface, but a nested class can be part of an interface. Any class you put inside an interface is automatically public and static. Since the class is static, it doesn’t violate the rules for interfaces—the nested class is only placed inside the namespace of the interface.

 

17) It's suggested to put a main() in every class to act as a test bed for that class. One drawback to this is the amount of extra compiled code you must carry around. If this is a problem, you can use a nested class to hold your test code (the main() method). The inner class will generate a OuterClassName$InnerClassName.class after compiled. You can use this class for testing, but you don’t need to include it in your shipping product.

 

18) It doesn’t matter how deeply an inner class may be nested—it can transparently access all of the static members of all the classes it is nested within.

 

19) Typically, the inner class inherits from a class or implements an interface, and the code in the inner class manipulates the outer class object that it was created within. So you could say that an inner class provides a kind of window into the outer class.

 

20) The most compelling reason for inner classes is: Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation. Without the ability that inner classes provide to inherit—in effect—from more than one concrete or abstract class, some design and programming problems would be intractable. So one way to look at the inner class is as the rest of the solution of the multiple-inheritance problem. Interfaces solve part of the problem, but inner classes effectively allow "multiple implementation inheritance." That is, inner classes effectively allow you to inherit from more than one non-interface.

Comment by Sean, why not use aggregation/composition instead of multiple inheritance?

 

21) If you didn’t need to solve the "multiple implementation inheritance" problem, you could conceivably code around everything else without the need for inner classes. But with inner classes you have these additional features:
    a. The inner class can have multiple instances, each with its own state information that is independent of the information in the outer class object.
    b. In a single outer class you can have several inner classes, each of which implements the same interface or inherits from the same class in a different way.

    c. The point of creation of the inner-class object is tied to the creation of the outer-class object.
    d. There is no potentially confusing "is-a" relationship with the inner class; it’s a separate entity.

 

22) Because the inner-class constructor must attach to a reference of the enclosing class object, things are slightly complicated when you inherit from an inner class. The problem is that the "secret" reference to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default object to attach to. Because the derived class is not an inner class, which does't require a instance of the outer class to instantialize. However the constructor of the derrived class must invoke the contructor of the super class, which is an inner class and needs an instance of its outer class to instanialize. You must use a special syntax to make the association explicitly in the constructor of the derrived class: enclosingClassReference.super();

 

23) Inner class cannot be overriden (constructors are static). If B extends A then A's inner class C can be described as B.C (same as A.C) and can be initilized via new B().new C(). But if B also has an inner class called C then B.C is specific to B's inner class C.

 

24) A local inner class cannot have an access specifier because it isn’t part of the outer class, but it does have access to the final variables in the current code block and all the members of the enclosing class.

 

25) Since the name of the local inner class is not accessible outside the method, the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor, since an anonymous inner class can only use instance initialization. Another reason to make a local inner class rather than an anonymous inner class is if you need to make more than one object of that class.

 

26) If inner classes are anonymous, the compiler simply starts generating numbers as inner class identifiers. If inner classes are nested within inner classes, their names are simply appended after a '$' and the outer class identifier.

分享到:
评论

相关推荐

    一本android的好书beginning android 2 和 源码

    ■Chapter 10: The Input Method Framework Keyboards, Hard and Soft Tailored to Your Needs Tell Android Where It Can Go Fitting In Unleash Your Inner Dvorak ■Chapter 11: Applying Menus Menus of ...

    基于SpringBoot+Vue的“智慧食堂”设计与实现(Java毕业设计,包括源码、数据库、教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)

    历届奥运会奖牌数据(1896-2024年).xlsx

    本次分享的数据为1896年-2024年(从雅典到巴黎)间奥运会奖牌数据,包括年份、届次、国家地区、名次、金牌、银牌、铜牌等数据,含免费下载链接 ## 一、数据介绍 数据名称:历届奥运会奖牌数据 数据范围:世界各国 样本数量:1877条 数据年份:1896年-2024年 数据说明:包括届次、国家、名次等数据

    基于SpringBoot+Vue的实习管理系统(Java毕业设计,包括源码、数据库、教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)

    【人机交互】MATLAB直车道线检测.zip

    【人机交互】MATLAB直车道线检测

    基于SSM+JSP的KTV点歌系统+数据库(Java毕业设计,包括源码,教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4

    基于SSM+JSP的课程在线教育资源管理系统(1)+数据库(Java毕业设计,包括源码,教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4

    2023年计算机硬件的组装实验报告.pdf

    2023年计算机硬件的组装实验报告.pdf

    springboot大学生租房系统 LW PPT.zip

    ava项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文

    Flutter分析:带有质量平衡部分机翼的MATLAB计算(含Elastic轴与中心对齐)

    内容概要:本文档主要针对含有质量平衡段(即弹性轴和重心重合点xa=0)的硬翼Flutter问题提供了MATLAB解决方案。文档通过迭代的方式对一系列参数(如频率比(fr)、弹性轴(E)和半径(r)等)进行操作,并利用贝塞尔函数(Kn)来评估flutter速度(UFhat),从而预测了不同质比(mu)下flutter的缩减速度变化情况。同时,文档包含了绘图命令以视觉展示减小颤振速度随质量比变化的趋势以及相应的MATLAB代码。 适合人群:航空工程、飞行器动力学领域的科研工作者,工程师及研究生。尤其是那些从事飞行安全性和稳定性研究的专业人士。 使用场景及目标:主要用于解决飞行器设计过程中遇到的具体颤振问题,能够为设计新型飞机或其他有翼飞行物体提供科学依据和技术支持。它还能够辅助教育,帮助相关专业的学生理解flutter现象及其预防措施。 其他说明:此文件是以数值方法探讨带质量平衡的翅膀颤振特性的实例,在工程上有着重要意义。对于希望深入学习此类问题的人来说,这是一个极好的参考资料和实验平台。然而,实际应用还需要进一步考虑真实条件下的复杂因素,因此需要更多的专业知识和背景资料的支持。

    基于JAVA的机场航班起降与协调管理系统&毕业设计&毕业论文&数据库&演示视频&源代码

    本次项目是设计一个基于JAVA的机场航班起降与协调管理系统。 (1)在经济可行性上来分析的话,该软件是机场内部使用的一个指挥协调软件,属于航空安全投资,本软件开发成本并不高,软件和服务器数据库可以用机场原有的数据库进行开发,比起空难给航空公司造成的损失来说九牛一毛。 (2)在技术可行性上来分析的话,该软件主要运用了Java技术、jQuery-easyui和Mysql数据库技术。Java是到目前来说最稳定的、最可靠的软件开发工具;jQuery-easyui虽然是比较新的前台开发技术,但是他的界面新颖整洁,适合于功能性软件的开发;Mysql数据库也是许多大公司都采用的软件项目开发数据库,不仅稳定而且性能可靠,可以用作本次软件的开发。 (3)在法律可行性上来分析的话,该软件使用的技术都为开源的软件开发工具和语言,虽然Java等开发技术都存在Sun公司的版权问题,但是Java技术是可以免费使用的,没有涉及到法律上的侵权。 (4)在方案可行性上来分析的话,此次软件开发的很大一部分精力都放在了软件的需求分析和设计方面,设计出来的软件可以很好地去实现我们所要完成的软件预先设计的功能。

    2023年计算机与通信网络实验报告.pdf

    2023年计算机与通信网络实验报告.pdf

    2023年四川省德阳市统招专升本计算机自考真题(含答案).pdf

    2023年四川省德阳市统招专升本计算机自考真题(含答案).pdf

    基于SSM+JSP的农产品供销服务系统+数据库(Java毕业设计,包括源码,教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4

    篮球论坛系统--论文.zip

    Java项目基于springboot的课程设计,包含源码+数据库+毕业论文

    基于SpringBoot+Vue的致远汽车租赁系统(Java毕业设计,包括源码、数据库、教程).zip

    Java 项目,仅供学习参考。 Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)

    GUI面板MATLAB答题纸试卷自动识别.zip

    GUI面板MATLAB答题纸试卷自动识别

    Java毕业设计-SpringBoot+Vue的班级综合测评管理系统(附源码、数据库、教程).zip

    Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)

    springboot大学生体质测试管理系统--论文.zip

    ava项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文

    美国机器人渗透度(工具变量).dta

    美国工业机器人渗透度是衡量自动化技术在制造业和其他工业领域应用程度的重要指标,可帮助研究制造业自动化趋势、劳动力市场变化,以及智能制造对经济的影响,同时,本分享数据也可用作工业机器人工具变量 ## 一、美国工业机器人渗透度数据的介绍 数据年份:2011-2019年 数据范围:美国各行业 数据格式:excel、dta

Global site tag (gtag.js) - Google Analytics