JSP 考点总结Word下载.docx
《JSP 考点总结Word下载.docx》由会员分享,可在线阅读,更多相关《JSP 考点总结Word下载.docx(22页珍藏版)》请在冰豆网上搜索。
int[]m[];
int[][]m;
数组的缺省值
与其它的变量不同,不论数组在向处创建,它总是使用可以使用缺省值。
示例:
publicclassMyAr{
publicstaticvoidmain(Stringargv[]){
int[]i=newint[5];
inti[5];
编译错误
int[]m[]={{1,2,3,4},{2,3,4,5},{4,5,6,7}};
int[][]n={{1,2,3,4},{2,3,4,5},{4,5,6}};
intj;
m=n;
for(intk=0;
k<
n.length;
k++){
System.out.println(n[k].length);
}
System.out.println(i[5]);
//!
运行错误,超界
System.out.println(i[4]);
//正确,打印0
System.out.println(j);
//!
编译错误,没有初始化
For(intk=0;
i.length;
I[k]=k;
Objective2,Declaringclassesandvariables
Declareclasses,innerclasses,methods,instancevariablesstatic,variablesandautomatic(methodlocal)variables,makingappropriateuseofallpermittedmodifiers(suchaspublicfinalstaticabstractandsoforth).Statethesignificanceofeachofthesemodifiersbothsinglyandincombinationandstatetheeffectofpackagerelationshipsondeclareditemsqualifiedbythesemodifiers.
目标2 声明类与变量
声明类,内部类,方法,实例变量,静态变量,自动变量(局部变量),正确使用各种修饰符(public,private,static,final,abstract)。
在JAVA中万事万物皆对象,即使JAVA程序本身也是对象。
类的定义和对象的生成
publicclassMyClass{
//类定义
inti;
floatf;
//类数据成员
voidamethod(){ //方法
//
局部变量
}
MyClassaClass=newMyClass();
//创建类的一个实例(对象)
修饰符说明
private
被了变量所在的类,其它任何类都不可以访问这个成员。
无访问修饰符
所谓的包访问权限,同一个包内的其它类可以访问这个成员。
Protected
与无访问修饰符具有权限外,还允许子类访问这个成员。
Public
具有全局可视性,任何其它类都可以访问这个成员。
Static
Static变量称为类变量,类的所有对象共享这个变量。
Static方法称为类方法,它只能访问static变量。
静态方法不能被子类overriding为非静态方法,但静态方法可以被子类静态方法所Hided.
Native
只用于方法,指明方法体是由其它编程语言编写的。
它以;
结尾不是以{}结尾。
Publicnativevoidfastcalc();
Abstract
Abstract修饰符可用于类与方法前,在用于方法前时表明方法不具备方法体。
只支类中包括了抽象方法则它必须声明为抽象类。
如果一个类实现一个接口时,它没有为接口中所有的方法提供实现时,也必须声明为抽象类。
Final
Final修饰符可用于类,方法和变量,fianl类不能被继承,所有final类中的方法都是教学final方法。
Final变量的值必须在创建时设定并具不能被修改。
Synchronized
防止多个线程同时访问相同的代码段。
Transient
表明类序列化时,变量不必序列化。
Volatile
表明变量由于线程可能异步修改。
abstractclassBase{
abstractpublicvoidmyfunc();
//抽象方法
publicvoidanother(){ //实例方法
System.out.println("
Anothermethod"
);
staticvoidsmethod(){ //类方法
basesmethod"
publicclassAbsextendsBase{
publicstaticvoidmain(Stringargv[]){
Absa=newAbs();
Baseb=newAbs();
a.amethod();
//
动态绑定,运行时调用
a.smethod();
//静态方法,编译时调用
b.smethod();
publicvoidmyfunc(){
Myfunc"
}
publicvoidamethod(){ //实现抽象方法
myfunc();
staticvoidsmethod(){
//hiding
父类静态方法
Abssmethod"
Objective3,Thedefaultconstructor
Foragivenclass,determineifadefaultconstructorwillbecreatedandifsostatetheprototypeofthatconstructor
目标3 缺省构造器
结定一个类,确定是否有缺省构造器
构造器是与类名相同的方法,并具没有返回值。
缺省构造器是一个不具有任何参数的构造器。
在你没有提供构造器的条件下,编译器为自动化为你提供一个缺省的构造器,但一旦你定义了任何一个构造器,编译器就不会为你提供缺省构造器。
在这种条件下,如果你使用不具有参数的构造器将有错误。
构造器可以有访问修饰符,但不能有native,abstract,static,synchronized和final修饰符。
classBase{
Base(inti){
this.i=i;
singleintconstructor"
voidBase(){
System.out.println(“invoidBase()”);
//Base(){}
publicclassConsextendsBase{
Cons(inti){}
//Cons(inti){super(i);
Basec=newBase();
//编译错误,没有构造函数
Consn=newCons(3);
//编译错误,Base没有无参数构造函数,voidBase()函数不是构造函数。
Objective4,Overloadingandoverriding
Statethelegalreturntypesforanymethodgiventhedeclarationsofallrelatedmethodsinthisorparentclasses.
目标4 重载与覆写
为所有在自己或父类中的相关方法声明有效的返回值,
相同类中的方法
当在同一个类中有多个方法具有相同的名称时,这个方法就被重载了。
只有参数的次序和类型是区分重载方法的依据,而返回值和参数的名称对区分重载方法没有贡献,所以,不能以返回值的不同来重载方法。
子类中的方法
可以在子类中重载父类的方法,只要新的重载方法具有不同的参数次序或类型。
当你在子类中的方法具有于父类中的方法相同的signature,则称子类覆写了父类的方法。
注意:
子类覆写父类方法,它的访问修饰符可以不同,但子类要具有比父类更加严格的访问权限。
静态方法不能被覆写只能被HIDED。
基本类型为参数的重载
基本类型可以自动进行窄化转型(narrowingconversion),在没有相应数据类型的重载方法,它的数据类型向上晋升。
publicvoidanother(inti){
Anotherintmethod"
+i);
//publicintanother(inti){}//!
编译错误,重复定义
publicvoidanother(doubled){
Anotherdoublemethod"
+d);
staticvoidsmethod(){
a.another(4);
a.another(4.9f);
// 注意:
它调用了覆写方法
b.another(4.9f);
它不调用覆写方法
publicvoidmyfunc(){
publicvoidamethod(){
voidanother(floatf){
Absanotherfloatmethod"
+f);
voidanother(inti){ //编译错误,访问权限弱化
Absanothermethod"
Absanotherintmethod"
Section2FlowcontrolandexceptionHandling
Objective1,Theifandswitchstatements
Writecodeusing
if
and
switch
statementsandidentifylegalargumenttypesforthesestatements.
目标1 if
和switch语句
使用if和switch编写代码并能区分有效的参数类型
if的条件语句中只能是boolean型的数据。
Intk=1;
If(k)
System.out.println(k);
If(k==1)System.out.println(k);
正确
Switch的条件语句中只能是int数据类型,或int的窄化数据类型也就是byte,
char,short
Longi;
Switch(i){
//错误
Case1:
..break;
Case2:
…break;
Default:
..
case语句的break的使用,在switch块中只有遇到break语句时,块才跳出。
Default语句不必总安排在switch语句的最后,但default语句不在最后时,要使用break.
三元操作符 ?
:
publicclassMySwitch{
MySwitchms=newMySwitch();
ms.amethod();
ms.ifmethod();
bytek=30;
//byte,char,short,int
switch(k){
default:
//Putthedefaultatthebottom,nothere
Thisisthedefaultoutput"
case10:
ten"
break;
case20:
twenty"
break;
publicvoidifmethod(){
intk=1;
booleanb;
//if(k=1)System.out.println("
One"
if(k==1)System.out.println("
one"
if(b=true)System.out.println("
true"
输出结果:
tryit
Objective2,looping,breakandcontinue
Writecodeusingallformsofloopsincludinglabeledandunlabeleduseofbreakandcontinueandstatethevaluestakenbyloopcountervariablesduringandafterloopexecution.
目标2 循环,break和continue
编写各种(不)具有label的break和continue的循环语句。
明确循环开始与结束时,循环记数器的值。
For
循环
语法:
for(initialization;
conditionalexpression;
increment)
逗号运算符
它只能用于for的控制表达式中。
for(inti=1,j=i+10;
i<
5;
i++,j=i*2)
do-while与while的区别在开do-while中的语句至少执行一次。
Goto与break,continue
虽然goto是java的保留字,但java而不使用它。
Java中的跳跃功能由break与continue提供。
在java中,惟一一个放置lable而能够产生效益的地点就是恰恰放在迭代语句前。
一般而论,break与continue只会中断当前的循环,而与label搭配,可以中断多层循环。
Label1:
Outeriteration{
Inneriteration{
…
//1
continue;
//2
….
Continuelabel1;
//3
Breaklabel1;
//4
1中断内层迭代,回到外层迭代
2将执行点移至内层迭代的起始处
3同时中断内层迭代,再从外层迭代开始
4同时中断内外层迭代,不再进行任何迭代
务必记下。
在java中使用label的惟一理由是跳过一个以上的嵌套层次。
publicclassLabeledWhil