ImageVerifierCode 换一换
格式:DOCX , 页数:57 ,大小:213.54KB ,
资源ID:3811955      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

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

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

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java学习笔记.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java学习笔记.docx

1、Java学习笔记 final关键字修饰(修饰类 、方法、属性)1、final类不能被继承,没有子类,final类中的方法默认是final的。2、final方法不能被子类的方法覆盖,但可以被继承。3、final成员变量表示常量,只能被赋值一次,赋值后值不再改变。 (对应C+中的const),修饰类成员属性必须执行初始化。4、final不能用于修饰构造方法。父类句柄无法调用子类的final方法(final方法无法继承),private也无法继承。1.Final变量:Final修饰变量:final将变量设置为常量,不可更改(任何情况),其中类成员属性必须初始化,局部变量可以不执行初始化。Final修

2、饰对象句柄:final将句柄变成一个常数,即进行声明时,必须将句柄初始化到一个具体的对象(注意:类成员属性必须初始化,局部变量可以缺省),而且永远不能将句柄变成指向另外一个对象。然而,对象本身是可以修改的。Final修饰数组:类似对象句柄,不能重新指向其他数组,但数组中的对象并不是常量,可以更改。不能由于某个成员属性是final,就认定它的值能在编译时期知道,可能是初始化为随机数。2.final方法:作用:给方法上锁。防止一个方法的行为在继承期间被覆盖,改写Java编译器能自动侦测是否嵌入一个final方法。类中所有的private方法都自动成为final。3.final类:作用 不希望这个类

3、有子类。所以一个final类中的所有方法都默认是final,不能被修改,但是属性可以被修改,不是final的。同样有效率损失。FinalArguments.javaclass Gizmo public void spin() public class FinalArguments void with(final Gizmo g) /g = new Gizmo(); /g is final,false void without(Gizmo g) g = new Gizmo(); /g not final,true g.spin(); /void f(final int i) i+; / Cant

4、 change / You can only read from a final primitive: int g(final int i) return i + 1; public static void main(String args) FinalArguments bf = new FinalArguments(); bf.without(null); bf.with(null); FinalData.javaclass Value int i = 1;public class FinalData / Can be compile-time constants(编译期间决定) fina

5、l int i1 = 9; static final int I2 = 99; / Typical public constant: public static final int I3 = 39; / Cannot be compile-time constants:(运行期间决定) final int i4 = (int)(Math.random()*20); static final int i5 = (int)(Math.random()*20); Value v1 = new Value(); final Value v2 = new Value(); /v2句柄是个常量 stati

6、c final Value v3 = new Value(); /! final Value v4; / Pre-Java 1.1 Error: / no initializer / Arrays: final int a = 1, 2, 3, 4, 5, 6 ; public void print(String id) System.out.println( id + : + i4 = + i4 + , i5 = + i5); public static void main(String args) FinalData fd1 = new FinalData(); /! fd1.i1+; /

7、 Error: cant change value fd1.v2.i+; / Object isnt constant!,句柄是常量,内容不是 fd1.v1 = new Value(); / OK - not final for(int i = 0; i fd1.a.length; i+) fd1.ai+; / Object isnt constant! /! fd1.v2 = new Value(); / Error: Cant,v2是final的 /! fd1.v3 = new Value(); / change handle,v3是final的 /! fd1.a = new int3;/

8、a是final的 fd1.print(fd1); System.out.println(Creating new FinalData); FinalData fd2 = new FinalData(); fd1.print(fd1); fd2.print(fd2); public class finalequals public static void main(String args) String a = hello2; final String b = hello; String d = hello; String c = b + 2; System.out.println(c=b+2

9、+c); String e = d + 2; System.out.println(e=d+2 +e); System.out.println(a = c); System.out.println(a = e); System.out.println(a.equals(e); /:c=b+2 hello2e=d+2 hello2truefalsetrue大家可以先想一下这道题的输出结果。为什么第一个比较结果为true,而第二个比较结果为fasle。这里面就是final变量和普通变量的区别了,当final变量是基本数据类型以及String类型时,如果在编译期间能知道它的确切值,则编译器会把它当做

10、编译期常量使用。也就是说在用到该final变量的地方,相当于直接访问的这个常量,不需要在运行时确定。这种和C语言中的宏替换有点像。因此在上面的一段代码中,由于变量b被final修饰,因此会被当做编译器常量,所以在使用到b的地方会直接将变量b 替换为它的值。而对于变量d的访问却需要在运行时通过链接来进行。想必其中的区别大家应该明白了,不过要注意,只有在编译期间能确切知道final变量值的情况下,编译器才会进行这样的优化。(2)static关键字(修饰属性(一定是类属性而不是局部变量)和方法,但是可以修饰内部类)类变量:(存储在堆里) 1.类变量:static int sum; 2.类变量被类内所

11、有实例共享 3.类变量又叫static变量,class variables static variables类方法:1.类方法:方法前static2.类方法可以被调用即使类实例不存在,即可以使用类名调用类方法; double root = Math.sqrt(453.0);3.不能从一个static 方法内部发出对于一个非static方法的调用,但是反过来可以。说明: 有些属性希望被所有对象共享,则必须将其声明为static属性,被static声明的 属性成为全局属性。声明为static的方法有以下几条限制:它们仅能调用其他的static方法。它们只能访问static数据。它们不能以任何方式引

12、用this或super。构造函数前面不能有访问修饰符,默认的是public,但可以写为private以及protected。构造函数不允许加static。Eg: test_staticpackage static_java;public class test_static public int x=0; protected double y=1; class testone testone() System.out.println(woshi); static class testtwo testtwo() System.out.print(n); /new textone();只能引用sta

13、tic方法或者内部类,不能引用外部类因为外部类非static /public int i=x;static类里面只能引用static属性 public int x; /this.x=1;static类里面不能用this以及super 先加载父类,再加载子类:class Person static System.out.println(person static); public Person(String str) System.out.println(person +str); class MyClass extends personStatic /先执行父类成员属性初始化 Person p

14、erson = new Person(MyClass);/(5) static System.out.println(myclass static);/(2) public MyClass() System.out.println(myclass constructor);/(6) public class personStatic Person person = new Person(Test);/(3) static System.out.println(test static);/(1) public personStatic() System.out.println(test cons

15、tructor);/(4) public static void main(String args) new MyClass();/有main方法时,要先于成员初始化执行 /:test staticmyclass staticperson staticperson Testtest constructorperson MyClassmyclass constructor类似地,我们还是来想一下这段代码的具体执行过程。首先加载Test类,因此会执行Test类中的static块。接着执行new MyClass(),而MyClass类还没有被加载,因此需要加载MyClass类。在加载MyClass类

16、的时候,发现MyClass类继承自Test类,但是由于Test类已经被加载了,所以只需要加载MyClass类,那么就会执行MyClass类的中的static块。在加载完之后,就通过构造器来生成对象。而在生成对象的时候,必须先初始化父类的成员变量,因此会执行Test中的Person person = new Person(),而Person类还没有被加载过,因此会先加载Person类并执行Person类中的static块,接着执行父类的构造器,完成了父类的初始化,然后就来初始化自身了,因此会接着执行MyClass中的Person person = new Person(),最后执行MyClass

17、的构造器。(3)superclass Person public static void prt(String s) System.out.println(s); Person() prt(A Person.); Person(String name) prt(A person name is: + name); public class Chinese extends Person Chinese() super(); / 调用父类构造函数(1) prt(A chinese.);/ (2) Chinese(String name) super(name);/ 调用父类具有相同形参的构造函数(

18、3) prt(his name is: + name);/(4) Chinese(String name, int age) this(name);/ 调用当前具有相同形参的构造函数(5) prt(his age is: + age);/(6) public static void main(String args) Chinese cn = new Chinese(); cn = new Chinese(kevin); cn = new Chinese(kevin, 22); /结果:A Person.A chinese.A person name is:kevinhis name is:k

19、evinA person name is:kevinhis name is:kevinhis age is:22在这段程序中,this和super不再是像以前那样用“.”连接一个方法或成员,而是直接在其后跟上适当的参数,因此它的意义也就有了变化。super后加参数的是用来调用父类中具有相同形式的构造函数,如1和2处。this后加参数则调用的是当前具有相同参数的构造函数,如3处。当然,在Chinese的各个重载构造函数中,this和super在一般方法中的各种用法也仍可使用,比如4处,你可以将它替换为“this.prt”(因为它继承了父类中的那个方法)或者是“super.prt”(因为它是父类中

20、的方法且可被子类访问),它照样可以正确运行。但这样似乎就有点画蛇添足的味道了。class Person public int c; private String name; private int age; protected void setName(String name) this.name=name; protected void setAge(int age) this.age=age; protected void print() System.out.println(Name=+name+ Age=+age); public class DemoSuper extends Per

21、son public void print() System.out.println(DemoSuper:); super.print(); public static void main(String args) DemoSuper ds=new DemoSuper(); ds.setName(kevin); ds.setAge(22); ds.print(); / DemoSuper:Name=kevin Age=22子类protected子类private在DemoSuper中,重新定义的print方法覆写了父类的print方法,它首先做一些自己的事情,然后调用父类的那个被覆写了的方法。

22、输出结果说明了这一点:DemoSuper:Name=kevin Age=22这样的使用方法是比较常用的。另外如果父类的成员可以被子类访问,那你可以像使用this一样使用它,用“super.父类中的成员名”的方式,但常常你并不是这样来访问父类中的成员名的。在构造函数中构造函数是一种特殊的方法,在对象初始化的时候自动调用。在构造函数中,this和super也有上面说的种种使用方式,并且它还有特殊的地方。注意(访问权限):父类protected的属性和方法,子类可以使用;父类private的属性和方法,子类无法使用;protected以及private的属性和方法在子类父类以外无法使用1、在派生类的

23、构造函数中没有指明基类构造函数时,java会自动在第一行加入对基类构造函数的默认调用Super()语句。2、当基类的构造函数需要参数时,则一定要在派生类的构造函数第一条语句明确调用基类代参数的构造函数super(args)。(4)继承(Inheritance): 派生derivation 继承作用:实现代码复用,减少开发时间,减少维护时间 Subclass:子类;baseclass:父类 父类private无法继承,protected可以继承,java不支持多根继承 基本知识: 1.子类有不同于父类的属性和方法 2.父类接收到的消息可以被子类接收到 3.子类和父类有相同的类型 执行顺序:有ex

24、tends父类的情况下,先执行父类static属性定义,然后进行属性声明,然后是是构造函数,然后是子类执行 基类子对象: 创建一个派生类对象时,它在其中包含了基类的一个“子对象”。 基类子对象必须初始化,有且只有一种方法。 在派生类的构造函数中,第一条语句调用基类的构造函数来初始化基类子对象。 如何实现基类子对象初始化:1. 在派生类的构造函数中没有指明基类构造函数时,java会自动在第一行加入对基类构造函数的默认调用Super()语句。2. 当基类的构造函数需要参数时,则一定要在派生类的构造函数第一条语句明确调用基类代参数的构造函数super(args)。3. 当基类的构造函数不需要参数时,

25、则在派生类的构造函数中可以不明确的调用super()。 What keyword does the subclass use to declare its superclass? Super.Sandwich.javaclass Meal Meal() System.out.println(Meal(); class Bread Bread() System.out.println(Bread(); class Cheese Cheese() System.out.println(Cheese(); class Lettuce Lettuce() System.out.println(Lett

26、uce(); class Lunch extends Meal Lunch() System.out.println(Lunch(); class PortableLunch extends Lunch PortableLunch() System.out.println(PortableLunch(); public class Sandwich Bread b = new Bread(); Cheese c = new Cheese(); Lettuce l = new Lettuce(); Sandwich() System.out.println(Sandwich(); public

27、static void main(String args) new Sandwich(); /构造方法按顺序声明/ Bread()Cheese()Lettuce()Sandwich()(5)抽象类(abstract class) 1.抽象类不能被初始化 2.有抽象方法必须要有抽象类3.抽象类中不一定要有抽象方法PS:抽象类中可以有普通方法,但是必须要有方法体4.普通子类继承抽象类必须把所有的抽象方法实现5.抽象方法:方法前加abstract,不实现6.抽象类不可以实例化(声明对象)7.抽象子类继承抽象父类,不可以实现父类抽象方法8.使用限制:abstract不能与final并列修饰同一个类。a

28、bstract 不能与private、static、final或native并列修饰同一个方法。9. 注意,抽象类和普通类的主要有三点区别:1)抽象方法必须为public或者protected(因为如果为private,则不能被子类继 承,子类便无法实现该方法),缺省情况下默认为public。2)抽象类不能用来创建对象;3)如果一个类继承于一个抽象类,则子类必须实现父类的抽象方法。如果子类没有实 现父类的抽象方法,则必须将子类也定义为为abstract类。 抽象方法是一种特殊的方法:它只有声明,而没有具体的实现。抽象方法的声明格式为:Abstract void fun();如果一个类含有抽象方

29、法,则称这个类为抽象类,抽象类必须在类前用abstract关键 字修饰。因为抽象类中含有无具体实现的方法,所以不能用抽象类创建对象。public class abstractclass abstract class ab abstract int text();/此处仅为抽象方法,没有方法的实现 对于一个父类,如果它的某个方法在父类中实现出来没有任何意义,必须根据子类的实 际需求来进行不同的实现,那么就可以将这个方法声明为abstract方法,此时这个类也成为abstract类了。 public class abstractclass abstract class ab abstract int text(); public static void main(String args) ab ab=new ab();/此处报错 (6)接口(interface): 必须放在extends之后class C extends B implements A创建

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1