ImageVerifierCode 换一换
格式:DOC , 页数:8 ,大小:54KB ,
资源ID:7983216      下载积分:10 金币
验证码下载
登录下载
邮箱/手机:
图形码:
验证码: 获取验证码
温馨提示:
支付成功后,系统会自动生成账号(用户名为邮箱或者手机号,密码是验证码),方便下次登录下载和查询订单;
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/7983216.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请。


权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4009-655-100;投诉/维权电话:18658249818。

注意事项

本文(innerclasses.doc)为本站上传会员【仙人****88】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

innerclasses.doc

1、内部类:一个内部类的定义是定义在另一个内部的类。 原因是: 1.一个内部类的对象能够访问创建它的对象的实现,包括私有数据。 2.对于同一个包中的其他类来说,内部类能够隐藏起来。 3.匿名内部类可以很方便的定义回调。 4.使用内部类可以非常方便的编写事件驱动程序。 提起Java内部类(Inner Class)可能很多人不太熟悉,实际上类似的概念在C++里也有,那就是嵌套类(Nested Class),关于这两者的区别与联系,在下文中会有对比。内部类从表面上看,就是在类中又定义了一个类(下文会看到,内部类可以在很多地方定义),而实际上并没有那么简单,乍看上去内部类似乎有些多余,它的

2、用处对于初学者来说可能并不是那么显著,但是随着对它的深入了解,你会发现Java的设计者在内部类身上的确是用心良苦。学会使用内部类,是掌握Java高级编程的一部分,它可以让你更优雅地设计你的程序结构。下面从以下几个方面来介绍:     * 第一次见面 public interface Contents {     int value(); } public interface Destination {     String readLabel(); } public class Goods {     private class Content impleme

3、nts Contents {         private int i = 11;         public int value() {             return i;         }     }     protected class GDestination implements Destination {         private String label;         private GDestination(String whereTo) {             label = whereTo;         }    

4、     public String readLabel() {             return label;         }     }     public Destination dest(String s) {         return new GDestination(s);     }     public Contents cont() {         return new Content();     } } class TestGoods {     public static void main(String[] args)

5、 {         Goods p = new Goods();         Contents c = p.cont();         Destination d = p.dest("Beijing");     } } 在这个例子里类Content和GDestination被定义在了类Goods内部,并且分别有着protected和private修饰符来控制访问级别。Content代表着Goods的内容,而GDestination代表着Goods的目的地。它们分别实现了两个接口Content和 Destination。在后面的main方法里,直接用 Contents c

6、和Destination d进行操作,你甚至连这两个内部类的名字都没有看见!这样,内部类的第一个好处就体现出来了??隐藏你不想让别人知道的操作,也即封装性。 同时,我们也发现了在外部类作用范围之外得到内部类对象的第一个方法,那就是利用其外部类的方法创建并返回。上例中的cont()和dest()方法就是这么做的。那么还有没有别的方法呢?当然有,其语法格式如下: outerObject=new outerClass(Constructor Parameters); outerClass.innerClass innerObject=outerObject.new InnerCla

7、ss(Constructor Parameters); 注意在创建非静态内部类对象时,一定要先创建起相应的外部类对象。至于原因,也就引出了我们下一个话题??     * 非静态内部类对象有着指向其外部类对象的引用 对刚才的例子稍作修改: public class Goods {     private valueRate=2;     private class Content implements Contents {         private int i = 11*valueRate;         public int value() {

8、             return i;         }     }     protected class GDestination implements Destination {         private String label;         private GDestination(String whereTo) {             label = whereTo;         }         public String readLabel() {             return label;         }   

9、  }     public Destination dest(String s) {         return new GDestination(s);     }     public Contents cont() {         return new Content();     } } 修改的部分用蓝色显示了。在这里我们给Goods类增加了一个private成员变量valueRate,意义是货物的价值系数,在内部类Content的方法value()计算价值时把它乘上。我们发现,value()可以访问valueRate,这也是内部类的第二个好处??一个内

10、部类对象可以访问创建它的外部类对象的内容,甚至包括私有变量!这是一个非常有用的特性,为我们在设计时提供了更多的思路和捷径。要想实现这个功能,内部类对象就必须有指向外部类对象的引用。 Java编译器在创建内部类对象时,隐式的把其外部类对象的引用也传了进去并一直保存着。这样就使得内部类对象始终可以访问其外部类对象,同时这也是为什么在外部类作用范围之外向要创建内部类对象必须先创建其外部类对象的原因。 有人会问,如果内部类里的一个成员变量与外部类的一个成员变量同名,也即外部类的同名成员变量被屏蔽了,怎么办?没事,Java里用如下格式表达外部类的引用: outerClass.this 有了

11、它,我们就不怕这种屏蔽的情况了。     * 静态内部类 和普通的类一样,内部类也可以有静态的。不过和非静态内部类相比,区别就在于静态内部类没有了指向外部的引用。这实际上和C++中的嵌套类很相像了,Java内部类与C++嵌套类最大的不同就在于是否有指向外部的引用这一点上,当然从设计的角度以及以它一些细节来讲还有区别。 除此之外,在任何非静态内部类中,都不能有静态数据,静态方法或者又一个静态内部类(内部类的嵌套可以不止一层)。不过静态内部类中却可以拥有这一切。这也算是两者的第二个区别吧。     * 局部内部类  是的,Java内部类也可以是局部的,它可以定义在一个

12、方法甚至一个代码块之内。 public class Goods1 {      public Destination dest(String s) {           class GDestination implements Destination {                private String label;                private GDestination(String whereTo) {                     label = whereTo;                }                

13、public String readLabel() { return label; }           }           return new GDestination(s);      }      public static void main(String[] args) {           Goods1 g= new Goods1();           Destination d = g.dest("Beijing");      } } 上面就是这样一个例子。在方法dest中我们定义了一个内部类,最后由这个方法返回这个内部类的对象。如果我们

14、在用一个内部类的时候仅需要创建它的一个对象并创给外部,就可以这样做。当然,定义在方法中的内部类可以使设计多样化,用途绝不仅仅在这一点。 下面有一个更怪的例子: public class Goods2{      private void internalTracking(boolean b) {           if(b) {                class TrackingSlip {                     private String id;                     TrackingSlip(String s) {  

15、                        id = s;                     }                     String getSlip() { return id; }                }                TrackingSlip ts = new TrackingSlip("slip");                String s = ts.getSlip();           }      }      public void track() { internalTracking(true)

16、 }      public static void main(String[] args) {           Goods2 g= new Goods2();           g.track();      } } 你不能在if之外创建这个内部类的对象,因为这已经超出了它的作用域。不过在编译的时候,内部类TrackingSlip和其他类一样同时被编译,只不过它由它自己的作用域,超出了这个范围就无效,除此之外它和其他内部类并没有区别。     * 匿名内部类  java的匿名内部类的语法规则看上去有些古怪,不过如同匿名数组一样,当你只需要创建一个类的对

17、象而且用不上它的名字时,使用内部类可以使代码看上去简洁清楚。它的语法规则是这样的: new interfacename(){......}; 或 new superclassname(){......}; 下面接着前面继续举例子: public class Goods3 {      public Contents cont(){           return new Contents(){                private int i = 11;                public int value() {                    

18、 return i;                }           };      } } 这里方法cont()使用匿名内部类直接返回了一个实现了接口Contents的类的对象,看上去的确十分简洁。 在java的事件处理的匿名适配器中,匿名内部类被大量的使用。例如在想关闭窗口时加上这样一句代码: frame.addWindowListener(new WindowAdapter(){      public void windowClosing(WindowEvent e){           System.exit(0);      } });

19、有一点需要注意的是,匿名内部类由于没有名字,所以它没有构造函数(但是如果这个匿名内部类继承了一个只含有带参数构造函数的父类,创建它的时候必须带上这些参数,并在实现的过程中使用super关键字调用相应的内容)。如果你想要初始化它的成员变量,有下面几种方法:    1. 如果是在一个方法的匿名内部类,可以利用这个方法传进你想要的参数,不过记住,这些参数必须被声明为final。    2. 将匿名内部类改造成有名字的局部内部类,这样它就可以拥有构造函数了。    3. 在这个匿名内部类中使用初始化代码块。      * 为什么需要内部类?  java内部类有什么好处?为什么需要

20、内部类? 首先举一个简单的例子,如果你想实现一个接口,但是这个接口中的一个方法和你构想的这个类中的一个方法的名称,参数相同,你应该怎么办?这时候,你可以建一个内部类实现这个接口。由于内部类对外部类的所有内容都是可访问的,所以这样做可以完成所有你直接实现这个接口的功能。 不过你可能要质疑,更改一下方法的不就行了吗? 的确,以此作为设计内部类的理由,实在没有说服力。 真正的原因是这样的,java中的内部类和接口加在一起,可以的解决常被C++程序员抱怨java中存在的一个问题??没有多继承。实际上,C++的多继承设计起来很复杂,而java通过内部类加上接口,可以很好的实现多继

21、承的效果。 The Java programming language allows you to define a class within another class. class OuterClass { static class StaticNestedClass { } class InnerClass { } } A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to ot

22、her members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. Static Nested Classes As with class methods and variables, a static nested class is associated with its outer class. And like static class method

23、s, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference. Static nested classes are accessed using the enclosing class name: OuterClass.StaticNestedClass For example, to create an object for

24、the static nested class, use this syntax: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and

25、fields. Also, because an inner class is associated with an instance, it cannot define any static members itself. An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you m

26、ust first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass.InnerClass innerObject = outerObject.new InnerClass(); Anonymous Class An anonymous class is essentially a local class without a name. Instead of defining a local class and t

27、hen instantiating it, you can often use an anonymous class to combine these two steps...  an anonymous class is defined by a Java expression, not a Java statement.  This means that an anonymous class definition can be included within a larger Java expression... an anonymous class is truly an inner

28、class, because an object of the anonymous class cannot exist in the absence of an object of the enclosing class. Unlike a local class, however, an anonymous class cannot define a constructor. As with member classes and local classes, anonymous classes cannot contain static members. As with local

29、variables and local classes, anonymous classes cannot be declared public, protected, private, or static.  In fact, no modifiers can be specified in the definition of an anonymous class. Syntax for anonymous classes new className(optional argument list){classBody} new interfaceName(){classBody}

30、 The result of executing this expression is that a new class that extends className is defined, a new object of the new class is instantiated, and the expression is replaced by a reference to the new object. Any arguments you specify between the parentheses following the superclass name in an

31、 anonymous class definition are implicitly passed to the superclass constructor local classes and anonymous classes are defined inside code blocks, which often place them inside methods and constructors, but you can also place them inside static initializer blocks and instance initializers. An ano

32、nymous class can be particularly useful in those cases where l There is no reason for an object of the anonymous class to exist in the absence of an object of the enclosing class. l There is no reason for an object of the anonymous class to exist outside a method of the enclosing class. l Meth

33、ods of the object of the anonymous class need access to members of the object of the enclosing class. l Methods of the object of the anonymous class need access to final local variables and method parameters belonging to the method in which the anonymous class is defined. l Only one instance of

34、the anonymous class is needed. l There is no need for the class to have a name that is accessible elsewhere in the program. As with member classes and local classes, anonymous classes cannot contain static members. As with local variables and local classes, anonymous classes cannot be declared

35、public, protected, private, or static. Local Classes As is the case for an object of a member class (discussed in the previous lesson), an object of a local class must be internally linked to an object of the enclosing class.  I will often refer to that object as the containing object, and make co

36、mments about the containment hierarchy. There is no such thing as a member interface, a local interface, or an anonymous interface. the most important benefit of local classes has to do with accessing the members of enclosing classes.  Just like with member classes, methods of a local class have

37、direct access to all the members of the enclosing classes, including private members.  Thus the use of local classes can sometimes eliminate the requirement to connect objects together via constructor parameters. The thing that separates local classes from member classes is the fact that local class

38、es have access to local variables in the scope in which the local class is defined. The methods in a local class can access local variables and method parameters only if they are declared final. l As with member classes, local classes cannot contain static members. l As with local variables, local classes cannot be declared public, protected, private, or static.  l A local class cannot have the same name as any of its enclosing classes.

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服