1Java2实用教程(第三版)清华大学出版社(编著耿祥义张跃平)例子源代码建议使用文档结构图(选择Word菜单→视图→文档结构图)21第一章Java语言入门例子1publicclassHello{publicstaticvoidmain(Stringargs[]){System.out.println(你好,很高兴学习Java);}}例子2publicclassPeople{intheight;Stringear;voidspeak(Strings){System.out.println(s);}}classA{publicstaticvoidmain(Stringargs[]){Peoplezhubajie;zhubajie=newPeople();zhubajie.height=170;zhubajie.ear=两只大耳朵;System.out.println(身高:+zhubajie.height);System.out.println(zhubajie.ear);zhubajie.speak(师傅,咱们别去西天了,改去月宫吧);}}例子3importjava.applet.*;importjava.awt.*;publicclassBoyextendsApplet{publicvoidpaint(Graphicsg){g.setColor(Color.red);g.drawString(我一边喝着咖啡,一边学Java呢,5,30);g.setColor(Color.blue);2g.drawString(我学得很认真,10,50);}}3第二章标识符、关键字和数据类型例子1publicclassExample2_1{publicstaticvoidmain(Stringargs[]){charchinaWord='你',japanWord='ぁ';intp1=36328,p2=38358;System.out.println(汉字\'你\'在unicode表中的顺序位置:+(int)chinaWord);System.out.println(日语\'ぁ\'在unicode表中的顺序位置:+(int)japanWord);System.out.println(unicode表中第20328位置上的字符是:+(char)p1);System.out.println(unicode表中第12358位置上的字符是:+(char)p2);}}例子2publicclassExample2_2{publicstaticvoidmain(Stringargs[]){intc=2200;longd=8000;floatf;doubleg=123456789.123456789;c=(int)d;f=(float)g;//导致精度的损失.System.out.print(c=+c);System.out.println(d=+d);System.out.println(f=+f);System.out.println(g=+g);}}例子3publicclassExample2_3{publicstaticvoidmain(Stringargs[]){inta[]={100,200,300};intb[]={10,11,12,13,14,15,16};b=a;b[0]=123456;System.out.println(数组a:+a[0]+,+a[1]+,+a[2]);图2.2基本数据的转换与输出4System.out.println(数组b:+b[0]+,+b[1]+,+b[2]);System.out.println(数组b的长度:+b.length);}}5第三章运算符、表达式和语句例子1classExample3_1{publicstaticvoidmain(Stringargs[]){chara1='十',a2='点',a3='进',a4='攻';charsecret='8';a1=(char)(a1^secret);a2=(char)(a2^secret);a3=(char)(a3^secret);a4=(char)(a4^secret);System.out.println(密文:+a1+a2+a3+a4);a1=(char)(a1^secret);a2=(char)(a2^secret);a3=(char)(a3^secret);a4=(char)(a4^secret);System.out.println(原文:+a1+a2+a3+a4);}}例子2classExample3_2{publicstaticvoidmain(Stringargs[]){intx,y=10;if(((x=0)==0)||((y=20)==20)){System.out.println(现在y的值是:+y);}inta,b=10;if(((a=0)==0)|((b=20)==20)){System.out.println(现在b的值是:+b);}}}例子3publicclassExample3_3{publicstaticvoidmain(Stringargs[]){inta=9,b=5,c=7,t;6if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}System.out.println(a=+a+,b=+b+,c=+c);}}例子4publicclassExample3_4{publicstaticvoidmain(Stringargs[]){intmath=65,english=85;if(math60){System.out.println(数学及格了);}else{System.out.println(数学不及格);}if(english90){System.out.println(英语是优);}else{System.out.println(英语不是优);}System.out.println(我在学习控制语句);}}例子5publicclassExample3_5{publicstaticvoidmain(Stringargs[]){intx=2,y=1;switch(x+y){case1:System.out.println(x+y);7break;case3:System.out.println(x+y);case0:System.out.println(x+y);break;default:System.out.println(没有般配的+(x+y));}}}例子6publicclassExample3_6{publicstaticvoidmain(Stringargs[]){longsum=0,a=5,item=a,n=10,i=1;for(i=1;i=n;i++){sum=sum+item;item=item*10+a;}System.out.println(sum);}}例子7classExample3_7{publicstaticvoidmain(Stringargs[]){doublesum=0,a=1;inti=1;while(i=20){sum=sum+a;i=i+1;a=a*(1.0/i);}System.out.println(sum=+sum);}}例子8classExample3_8{publicstaticvoidmain(Stringargs[]){intsum=0,i,j;for(i=1;i=10;i++)8{if(i%2==0)//计算1+3+5+7+9continue;sum=sum+i;}System.out.println(sum=+sum);for(j=2;j=50;j++)//求50以内的素数{for(i=2;i=j/2;i++){if(j%i==0)break;}if(ij/2){System.out.println(+j+是素数);}}}}9第四章类、对象和接口例子1classXiyoujiRenwu{floatheight,weight;Stringhead,ear,hand,foot,mouth;voidspeak(Strings){System.out.println(s);}}classA{publicstaticvoidmain(Stringargs[]){XiyoujiRenwuzhubajie;//声明对象zhubajie=newXiyoujiRenwu();//为对象分配内存,使用new运算符和默认的构造方法}}例子2classPoint{intx,y;Point(inta,intb){x=a;y=b;}}publicclassA{publicstaticvoidmain(Stringargs[]){Pointp1,p2;//声明对象p1和p2p1=newPoint(10,10);//为对象分配内存,使用new和类中的构造方法p2=newPoint(23,35);//为对象分配内存,使用new和类中的构造方法}}例子3classXiyoujiRenwu{floatheight,weight;Stringhead,ear,hand,foot,mouth;voidspeak(Strings){head=歪着头;System.out.println(s);10}}classExample4_3{publicstaticvoidmain(Stringargs[]){XiyoujiRenwuzhubajie,sunwukong;//声明对象zhubajie=newXiyoujiRenwu();//为对象分配内存sunwukong=newXiyoujiRenwu();zhubajie.height=1.80f;//对象给自己的变量赋值zhubajie.head=大头;zhubajie.ear=一双大耳朵;sunwukong.height=1.62f;//对象给自己的变量赋值sunwukong.weight=1000f;sunwukong.head=绣发飘飘;System.out.println(zhubajie的身高:+zhubajie.height);System.out.println(zhubajie的头:+zhubajie.head);System.out.println(sunwukong的重量:+sunwukong.weight);System.out.println(sunwukong的头:+sunwukong.head);zhubajie.speak(俺老猪我想娶媳妇);//对象调用方法System.out.println(zhubajie现在的头:+zhubajie.head);sunwukong.speak(老孙我重1000斤,我想骗八戒背我);//对象调用方法System.out.println(sunwukong现在的头:+sunwukong.head);}}例子4class梯形{float上底,下底,高,面积;梯形(floatx,floaty,floath){上底=x;下底=y;高=h;}float计算面积(){面积=(上底+下底)*高/2.0f;return面积;}void修改高(floatheight){高=height;}float获取高(){return高;}}11publicclassExample4_4{publicstaticvoidmain(Stringargs[]){梯形laderOne=new梯形(12.0f,3.5f,50),laderTwo=new梯形(2.67f,3.0f,10);System.out.println(laderOne的高是:+laderOne.获取高());System.out.println(laderTwo的高是:+laderTwo.获取高());System.out.println(laderOn