欢迎来到天天文库
浏览记录
ID:37081012
大小:134.50 KB
页数:22页
时间:2019-05-16
《Java程序设计基础教程-杨佩理(源程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Java程序设计基础教程-杨佩理(源程序&PDF课件)例4-1classAbc{staticintnum=0;voidcount(){num++;System.out.println("Thisisobject"+num);}publicstaticvoidmain(String[]args){Abca=newAbc();Abcb=newAbc();Abcc=newAbc();a.count();b.count();c.count();System.out.println(Abc.num);}}例4-2classCircle{doubler;doublegetL(
2、){return2*Math.PI*r;//Math.PI为圆周率}doublegetS(){returnMath.PI*r*r;}}例4-3classMyCal{staticintgetSum(intn){intsum=0;for(inti=1;i<=n;i++)sum+=i;returnsum;}staticlonggetFac(intn){longfac=1;for(inti=1;i<=n;i++)fac*=i;returnfac;}}classTest{publicstaticvoidmain(String[]aa){System.out.println(
3、MyCal.getSum(100)+"t"+MyCal.getFac(10));}}例4-4classPerson{intsex;intsalary;voidemployed(){System.out.print("Workstate:");if(salary==0)System.out.println("nojob");elseSystem.out.println("job");}}classTest{publicstaticvoidmain(String[]aa){Personps=newPerson();ps.salary=300;ps.employed(
4、);ps.salary=0;ps.employed();}}例4-5classReferenceTest{publicstaticvoidmain(String[]args){Personp1=newPerson();//设对象为A,p1指向APersonp2=newPerson();//设对象为B,p2指向Bp1.salary=1;p2.salary=2;System.out.println(p1.salary+"t"+p2.salary);p1=p2;//p1重定向,也指向了p2指向的BSystem.out.println(p1.salary+"t"+p2
5、.salary);p1.salary=8;System.out.println(p1.salary+"t"+p2.salary);}}例4-6classStar{staticvoidprint(charc,intn){for(inti=0;i6、-7classTestCircle{publicstaticvoidmain(String[]args){Circlecc=newCircle();cc.r=3.5;System.out.println(cc.getL()+"tt"+cc.getS());}}例4-8classTestOL{intx;voidfunc(){System.out.println("noarg");}Stringfunc(inti){return"argis"+i;}voidfunc(Strings){System.out.println(s);}publicstaticvoidm7、ain(String[]a){TestOLc=newTestOL();c.func();c.func(c.func(100));}}例4-9classTestOL1{voidfunc(doublex){System.out.println(x+"isdouble");}voidfunc(intx){System.out.println(x+"isint");}publicstaticvoidmain(String[]a){TestOL1c=newTestOL1();c.func(1);//将调用voidfunc(intx)c.func(2.5);//将调用void8、func(
6、-7classTestCircle{publicstaticvoidmain(String[]args){Circlecc=newCircle();cc.r=3.5;System.out.println(cc.getL()+"tt"+cc.getS());}}例4-8classTestOL{intx;voidfunc(){System.out.println("noarg");}Stringfunc(inti){return"argis"+i;}voidfunc(Strings){System.out.println(s);}publicstaticvoidm
7、ain(String[]a){TestOLc=newTestOL();c.func();c.func(c.func(100));}}例4-9classTestOL1{voidfunc(doublex){System.out.println(x+"isdouble");}voidfunc(intx){System.out.println(x+"isint");}publicstaticvoidmain(String[]a){TestOL1c=newTestOL1();c.func(1);//将调用voidfunc(intx)c.func(2.5);//将调用void
8、func(
此文档下载收益归作者所有